From e552fb6de5a5a092b3941092e930c0be93396140 Mon Sep 17 00:00:00 2001 From: Diana Date: Wed, 23 Oct 2024 10:06:59 +0200 Subject: [PATCH] Added my plot functions --- code/plot_functions.py | 59 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/code/plot_functions.py b/code/plot_functions.py index 4eb73ee..4d78cdd 100644 --- a/code/plot_functions.py +++ b/code/plot_functions.py @@ -66,4 +66,61 @@ def power_spectrum_plot(f, p): ax.set_xlim(0, 1000) plt.show() -####### ADD DIANAS POWER SPECTRUM PLOT \ No newline at end of file +####### ADD DIANAS POWER SPECTRUM PLOT +def plot_highlighted_integrals(frequency, power, exceeding_points, delta, threshold, color_mapping, points_categories): + """ + Plot the power spectrum and highlight integrals that exceed the threshold. + + Parameters + ---------- + frequency : np.array + An array of frequencies corresponding to the power values. + power : np.array + An array of power spectral density values. + exceeding_points : list + A list of harmonic frequencies that exceed the threshold. + delta : float + Half-width of the range for integration around each point. + threshold : float + Threshold value to compare integrals with local mean. + color_mapping : dict + A dictionary mapping each category to its color. + points_categories : dict + A mapping of categories to lists of points. + + Returns + ------- + fig : matplotlib.figure.Figure + The created figure object with highlighted integrals. + """ + fig, ax = plt.subplots() + ax.plot(frequency, power) # Plot power spectrum + + for point in exceeding_points: + integral, local_mean = calculate_integral(frequency, power, point, delta) + valid, _ = valid_integrals(integral, local_mean, threshold, point) + if valid: + # Define color based on the category of the point + color = next((c for cat, c in color_mapping.items() if point in points_categories[cat]), 'gray') + # Shade the region around the point where the integral was calculated + ax.axvspan(point - delta, point + delta, color=color, alpha=0.3, label=f'{point:.2f} Hz') + print(f"Integral around {point:.2f} Hz: {integral:.5e}") + + + # Define left and right boundaries of adjacent regions + left_boundary = frequency[np.where((frequency >= point - 5 * delta) & (frequency < point - delta))[0][0]] + right_boundary = frequency[np.where((frequency > point + delta) & (frequency <= point + 5 * delta))[0][-1]] + + # Add vertical dashed lines at the boundaries of the adjacent regions + ax.axvline(x=left_boundary, color="k", linestyle="--") + ax.axvline(x=right_boundary, color="k", linestyle="--") + + + ax.set_xlim([0, 1200]) + ax.set_xlabel('Frequency (Hz)') + ax.set_ylabel('Power') + ax.set_title('Power Spectrum with Highlighted Integrals') + ax.legend() + + return fig +