@@ -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 )
0 commit comments