From 528823f317c5ccb0b444b6d7037cb049a070562b Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 24 Oct 2024 17:02:14 +0200 Subject: [PATCH] Changed dianas plot --- code/plot_functions.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/code/plot_functions.py b/code/plot_functions.py index 1de4a75..8c3cebe 100644 --- a/code/plot_functions.py +++ b/code/plot_functions.py @@ -71,6 +71,11 @@ def power_spectrum_plot(f, p): functions_path = r"C:\Users\diana\OneDrive - UT Cloud\Master\GPs\GP1_Grewe\Projekt\gpgrewe2024\code" sys.path.append(functions_path) import useful_functions as u +import matplotlib.ticker as ticker + +def float_formatter(x, _): + """Format the y-axis values as floats with a specified precision.""" + return f'{x:.5f}' def plot_highlighted_integrals(ax, frequency, power, points, color_mapping, points_categories, delta=2.5): """ @@ -98,7 +103,7 @@ def plot_highlighted_integrals(ax, frequency, power, points, color_mapping, poin None """ ax.plot(frequency, power) # Plot power spectrum on the existing axes - + for point in points: # Calculate the integral and local mean integral, local_mean = u.calculate_integral_2(frequency, power, point) @@ -108,23 +113,24 @@ def plot_highlighted_integrals(ax, frequency, power, points, color_mapping, poin if valid: # Define color based on the category of the point + point_category = next((cat for cat, pts in points_categories.items() if point in pts), "Unknown") 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') - - # 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.axvspan(point - delta, point + delta, color=color, alpha=0.3, label=f'{point_category}') # 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.text(f"{point_category}: Integral: {integral:.5e}, Color: {color}") + ax.set_xlim([0, 1200]) + ax.set_ylim([0, 6e-5]) ax.set_xlabel('Frequency (Hz)') ax.set_ylabel('Power') ax.set_title('Power Spectrum with Highlighted Integrals') - #ax.legend() + + # Apply float formatting to the y-axis + ax.yaxis.set_major_formatter(ticker.FuncFormatter(float_formatter)) +