Skip to content

Commit 123125c

Browse files
committed
Fixed another plotpy V1 regression (thx to guiqwt)
Fixes aspect ratio issue when doing autoscale
1 parent 149e7a0 commit 123125c

1 file changed

Lines changed: 23 additions & 28 deletions

File tree

plotpy/plot/base.py

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -396,23 +396,6 @@ def __init__(
396396
self.set_aspect_ratio(options.aspect_ratio, options.lock_aspect_ratio)
397397
self.replot() # Workaround for the empty image widget bug
398398

399-
def replot(self) -> None:
400-
"""
401-
Redraw the plot
402-
403-
If the `autoReplot` option is not set (which is the default)
404-
or if any curves are attached to raw data, the plot has to
405-
be refreshed explicitly in order to make changes visible.
406-
407-
.. seealso::
408-
409-
:py:meth:`qwt.plot.QwtPlot.updateAxes`,
410-
:py:meth:`qwt.plot.QwtPlot.setAutoReplot`
411-
"""
412-
super().replot()
413-
if self.lock_aspect_ratio:
414-
self.apply_aspect_ratio()
415-
416399
# ---- Private API ----------------------------------------------------------
417400
def __del__(self):
418401
# Sometimes, an obscure exception happens when we quit an application
@@ -1937,11 +1920,9 @@ def do_autoscale(self, replot: bool = True, axis_id: int | None = None) -> None:
19371920
vmax = 10 ** (np.log10(vmax) + 0.002 * dv)
19381921
self.set_axis_limits(axis_id, vmin, vmax)
19391922
self.setAutoReplot(auto)
1940-
19411923
self.updateAxes()
1942-
# if self.lock_aspect_ratio:
1943-
# self.replot()
1944-
# self.apply_aspect_ratio(full_scale=True)
1924+
if self.lock_aspect_ratio:
1925+
self.apply_aspect_ratio(full_scale=True)
19451926
if replot:
19461927
self.replot()
19471928
self.SIG_PLOT_AXIS_CHANGED.emit(self)
@@ -2185,6 +2166,9 @@ def apply_aspect_ratio(self, full_scale: bool = False) -> None:
21852166
21862167
Args:
21872168
full_scale: if True, the aspect ratio is applied to the full scale
2169+
(this argument is True only when doing autoscale, i.e. in method
2170+
:py:meth:`do_autoscale`: it is necessary to ensure that the whole
2171+
image items are visible after autoscale, whatever their aspect ratio)
21882172
"""
21892173
if not self.isVisible():
21902174
return
@@ -2206,19 +2190,30 @@ def apply_aspect_ratio(self, full_scale: bool = False) -> None:
22062190
if w == 0 or h == 0:
22072191
return # avoid division by zero
22082192

2193+
# Compute new Y limits, keeping the same X limits and aspect ratio
22092194
dy2 = (h * dx1) / (w * self.__aspect_ratio)
2210-
dx2 = (w * dy1 * self.__aspect_ratio) / h
22112195

2212-
fix_yaxis = not full_scale or dy2 > dy1
2196+
# Compute new X limits, keeping the same Y limits and aspect ratio
2197+
dx2 = (w * dy1 * self.__aspect_ratio) / h
22132198

2214-
if fix_yaxis:
2215-
delta_y = 0.5 * (dy2 - dy1)
2216-
y0 -= delta_y
2217-
y1 += delta_y
2218-
else:
2199+
if full_scale and dy2 <= dy1:
2200+
# If the new Y limits are smaller than the current ones,
2201+
# *and* if we are doing autoscale, we keep the current Y limits.
2202+
# Why? Because, if the new Y limits are smaller than the current ones,
2203+
# it means that the image items would not be entirely visible after
2204+
# autoscale, and we don't want that.
22192205
delta_x = 0.5 * (dx2 - dx1)
22202206
x0 -= delta_x
22212207
x1 += delta_x
2208+
else:
2209+
# If we are not doing autoscale, then we only change the Y limits (that
2210+
# is a choice, we could also change the X limits).
2211+
# If we are doing autoscale, then we change the Y limits only if the
2212+
# new Y limits are larger than the current ones, in order to ensure
2213+
# that the image items are entirely visible after autoscale.
2214+
delta_y = 0.5 * (dy2 - dy1)
2215+
y0 -= delta_y
2216+
y1 += delta_y
22222217

22232218
self.set_plot_limits(x0, x1, y0, y1)
22242219

0 commit comments

Comments
 (0)