From a9378771acfc9ae96096c61de9ed00c3f3f33761 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 24 Oct 2024 15:45:41 +0200 Subject: [PATCH] Changes dianas plot function --- code/plot_functions.py | 39 ++++++++++++++------------------------- 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/code/plot_functions.py b/code/plot_functions.py index 5fb4c17..1de4a75 100644 --- a/code/plot_functions.py +++ b/code/plot_functions.py @@ -72,12 +72,14 @@ functions_path = r"C:\Users\diana\OneDrive - UT Cloud\Master\GPs\GP1_Grewe\Proje sys.path.append(functions_path) import useful_functions as u -def plot_highlighted_integrals(frequency, power, points, color_mapping, points_categories, delta=2.5): +def plot_highlighted_integrals(ax, frequency, power, points, color_mapping, points_categories, delta=2.5): """ - Plot the power spectrum and highlight integrals that exceed the threshold. + Highlight integrals on the existing axes of the power spectrum. Parameters ---------- + ax : matplotlib.axes.Axes + The axes on which to plot the highlighted integrals. frequency : np.array An array of frequencies corresponding to the power values. power : np.array @@ -93,50 +95,37 @@ def plot_highlighted_integrals(frequency, power, points, color_mapping, points_c Returns ------- - fig : matplotlib.figure.Figure - The created figure object with highlighted integrals. + None """ - fig, ax = plt.subplots() - ax.plot(frequency, power) # Plot power spectrum + ax.plot(frequency, power) # Plot power spectrum on the existing axes for point in points: - # Use the imported function to calculate the integral and local mean - integral, local_mean, _ = u.calculate_integral(frequency, power, point) + # Calculate the integral and local mean + integral, local_mean = u.calculate_integral_2(frequency, power, point) - # Use the imported function to check if the point is valid + # Check if the point is valid valid = u.valid_integrals(integral, local_mean, 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') - # Find the category of the point - point_category = next((cat for cat, pts in points_categories.items() if point in pts), "Unknown") - # 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 out point, category, and color - print(f"{point_category}: Integral: {integral:.5e}, Color: {color}") - # Annotate the plot with the point and its color ax.text(point, max(power) * 0.9, f'{point:.2f}', color=color, fontsize=10, ha='center') - # 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="--") + # Print out point, category, and color + point_category = next((cat for cat, pts in points_categories.items() if point in pts), "Unknown") + print(f"{point_category}: Integral: {integral:.5e}, Color: {color}") 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, ax + #ax.legend() +