Changes dianas plot function

This commit is contained in:
Diana 2024-10-24 15:45:41 +02:00
parent edc8d832e1
commit a9378771ac

View File

@ -72,12 +72,14 @@ functions_path = r"C:\Users\diana\OneDrive - UT Cloud\Master\GPs\GP1_Grewe\Proje
sys.path.append(functions_path) sys.path.append(functions_path)
import useful_functions as u 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 Parameters
---------- ----------
ax : matplotlib.axes.Axes
The axes on which to plot the highlighted integrals.
frequency : np.array frequency : np.array
An array of frequencies corresponding to the power values. An array of frequencies corresponding to the power values.
power : np.array power : np.array
@ -93,50 +95,37 @@ def plot_highlighted_integrals(frequency, power, points, color_mapping, points_c
Returns Returns
------- -------
fig : matplotlib.figure.Figure None
The created figure object with highlighted integrals.
""" """
fig, ax = plt.subplots() ax.plot(frequency, power) # Plot power spectrum on the existing axes
ax.plot(frequency, power) # Plot power spectrum
for point in points: for point in points:
# Use the imported function to calculate the integral and local mean # Calculate the integral and local mean
integral, local_mean, _ = u.calculate_integral(frequency, power, point) 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) valid = u.valid_integrals(integral, local_mean, point)
if valid: if valid:
# Define color based on the category of the point # 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') 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 # 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') 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 # 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') ax.text(point, max(power) * 0.9, f'{point:.2f}', color=color, fontsize=10, ha='center')
# Define left and right boundaries of adjacent regions # Print out point, category, and color
left_boundary = frequency[np.where((frequency >= point - 5 * delta) & (frequency < point - delta))[0][0]] point_category = next((cat for cat, pts in points_categories.items() if point in pts), "Unknown")
right_boundary = frequency[np.where((frequency > point + delta) & (frequency <= point + 5 * delta))[0][-1]] print(f"{point_category}: Integral: {integral:.5e}, Color: {color}")
# 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_xlim([0, 1200])
ax.set_xlabel('Frequency (Hz)') ax.set_xlabel('Frequency (Hz)')
ax.set_ylabel('Power') ax.set_ylabel('Power')
ax.set_title('Power Spectrum with Highlighted Integrals') ax.set_title('Power Spectrum with Highlighted Integrals')
ax.legend() #ax.legend()
return fig, ax