Skip to content

Commit a73e01b

Browse files
committed
Fixes #3: ZeroDivisionError when shrinking PlotWidget below its min width
1 parent f1c76fb commit a73e01b

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

plotpy/plot/base.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2172,17 +2172,22 @@ def get_plot_limits(
21722172
return x0, x1, y0, y1
21732173

21742174
# ---- Image scale/aspect ratio -related API -------------------------------
2175-
def get_current_aspect_ratio(self) -> float:
2175+
def get_current_aspect_ratio(self) -> float | None:
21762176
"""Return current aspect ratio
21772177
21782178
Returns:
2179-
float: the current aspect ratio
2179+
The current aspect ratio or None if the aspect ratio cannot be computed
2180+
(this happens when the plot has been shrunk to a size so that the
2181+
width is zero)
21802182
"""
21812183
dx = self.axisScaleDiv(self.X_BOTTOM).range()
21822184
dy = self.axisScaleDiv(self.Y_LEFT).range()
21832185
h = self.canvasMap(self.Y_LEFT).pDist()
21842186
w = self.canvasMap(self.X_BOTTOM).pDist()
2185-
return fabs((h * dx) / (w * dy))
2187+
try:
2188+
return fabs((h * dx) / (w * dy))
2189+
except ZeroDivisionError:
2190+
return None
21862191

21872192
def get_aspect_ratio(self) -> float:
21882193
"""Return aspect ratio
@@ -2220,7 +2225,9 @@ def apply_aspect_ratio(self, full_scale: bool = False) -> None:
22202225
if not self.isVisible():
22212226
return
22222227
current_aspect = self.get_current_aspect_ratio()
2223-
if abs(current_aspect - self.__aspect_ratio) < self.EPSILON_ASPECT_RATIO:
2228+
if current_aspect is None or (
2229+
abs(current_aspect - self.__aspect_ratio) < self.EPSILON_ASPECT_RATIO
2230+
):
22242231
return
22252232
ymap = self.canvasMap(self.Y_LEFT)
22262233
xmap = self.canvasMap(self.X_BOTTOM)

plotpy/tests/features/test_manager.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
# guitest: show
99

1010
from guidata.qthelpers import qt_app_context, win32_fix_title_bar_background
11+
from qtpy import QtCore as QC
1112
from qtpy import QtWidgets as QW
1213

1314
from plotpy.builder import make
@@ -45,6 +46,12 @@ def __init__(self, parent):
4546
def register_tools(self):
4647
self.manager.register_all_image_tools()
4748

49+
def minimumSizeHint(self):
50+
"""Override QWidget method"""
51+
# The purpose of this reimplementation is to trigger the issue #3
52+
# (see https://github.com/PlotPyStack/PlotPy/issues/3)
53+
return QC.QSize(150, 100)
54+
4855

4956
class Window(QW.QMainWindow):
5057
def __init__(self):
@@ -75,5 +82,19 @@ def test_manager():
7582
win.show()
7683

7784

85+
def test_resize_to_minimum_width():
86+
"""Testing resize window to minimum width"""
87+
# This test was failing at the time of Issue #3
88+
# (see https://github.com/PlotPyStack/PlotPy/issues/3)
89+
with qt_app_context(exec_loop=False):
90+
win = Window()
91+
win.show()
92+
width = 350
93+
while width > win.minimumWidth():
94+
width -= 1
95+
win.resize(width, 100)
96+
97+
7898
if __name__ == "__main__":
7999
test_manager()
100+
# test_resize_to_minimum_width()

0 commit comments

Comments
 (0)