autoscale works now also correctly in logarithmic plots (Fixes #8)

This commit is contained in:
2026-03-19 11:26:49 +01:00
parent 26d788a25a
commit ee389d8290
+24 -2
View File
@@ -2959,6 +2959,7 @@ class MonitorWidgets:
if self.__rescale: if self.__rescale:
x_scale = self.display_x_scaling_combobox.get_active_text( ) x_scale = self.display_x_scaling_combobox.get_active_text( )
y_scale = self.display_y_scaling_combobox.get_active_text( ) y_scale = self.display_y_scaling_combobox.get_active_text( )
# x-axis
if x_scale == "lin": if x_scale == "lin":
self.matplot_axes.set_xscale( "linear" ) self.matplot_axes.set_xscale( "linear" )
self.matplot_axes.set_xlim(xmin, xmax) self.matplot_axes.set_xlim(xmin, xmax)
@@ -2971,7 +2972,7 @@ class MonitorWidgets:
#self.display_autoscaling_checkbutton.set_active(True) #partial fix T141 #self.display_autoscaling_checkbutton.set_active(True) #partial fix T141
self.matplot_axes.set_xlim(xmax, xmin) self.matplot_axes.set_xlim(xmax, xmin)
#self.matplot_axes.invert_xaxis() #self.matplot_axes.invert_xaxis()
# y-axis
if y_scale == "lin": if y_scale == "lin":
self.matplot_axes.set_yscale( "linear" ) self.matplot_axes.set_yscale( "linear" )
self.matplot_axes.set_ylim(ymin, ymax) self.matplot_axes.set_ylim(ymin, ymax)
@@ -2987,8 +2988,29 @@ class MonitorWidgets:
if xlim_min != xmin or xlim_max != xmax: if xlim_min != xmin or xlim_max != xmax:
self.matplot_axes.set_xlim(xmin, xmax) self.matplot_axes.set_xlim(xmin, xmax)
# Rescale if new max is much larger than old ymax, simialar rules apply to ymin
ylim_min, ylim_max = self.matplot_axes.get_ylim() ylim_min, ylim_max = self.matplot_axes.get_ylim()
# Check if y-axis is log scale
yscale = self.matplot_axes.get_yscale()
if yscale == 'log' or yscale == 'symlog':
# For log scales, work with log values to avoid issues with
# small numbers (undetected rescale)
# Use log-space calculations
log_ymin, log_ymax = numpy.log10(ymin), numpy.log10(ymax)
log_diff = log_ymax - log_ymin
# Add a small margin in log space (about 0.08 in log10 units ~20% margin)
new_ymin = 10**(log_ymin - 0.08 * max(log_diff, 1))
new_ymax = 10**(log_ymax + 0.08 * max(log_diff, 1))
# Only update if values would change significantly
if (ylim_max < ymax or ylim_min > ymin or
ylim_max > new_ymax * 1.2 or ylim_min < new_ymin / 1.2):
self.matplot_axes.set_ylim(new_ymin, new_ymax)
else:
# Original linear scale logic
ydiff = ymax - ymin ydiff = ymax - ymin
if (ylim_max < ymax or ylim_min > ymin or if (ylim_max < ymax or ylim_min > ymin or
ylim_max > ymax + 0.2 * ydiff or ylim_min < ymin - 0.2 * ydiff): ylim_max > ymax + 0.2 * ydiff or ylim_min < ymin - 0.2 * ydiff):