@@ -78,6 +78,27 @@ def _has_blended_transform(transform):
7878 return _find_blended_transform (transform ) is not None
7979
8080
81+ def _is_axis_aligned_rectangle (points ):
82+ """Check whether polygon vertices trace an axis-aligned rectangle: every
83+ vertex is a corner of the bounding box and all four corners appear."""
84+ xs = [float (x ) for x , _ in points ]
85+ ys = [float (y ) for _ , y in points ]
86+ if not xs :
87+ return False
88+ x_min , x_max = min (xs ), max (xs )
89+ y_min , y_max = min (ys ), max (ys )
90+ corners = set ()
91+ for x , y in zip (xs , ys ):
92+ at_x_min = math .isclose (x , x_min , abs_tol = 1e-9 )
93+ at_x_max = math .isclose (x , x_max , abs_tol = 1e-9 )
94+ at_y_min = math .isclose (y , y_min , abs_tol = 1e-9 )
95+ at_y_max = math .isclose (y , y_max , abs_tol = 1e-9 )
96+ if not (at_x_min or at_x_max ) or not (at_y_min or at_y_max ):
97+ return False
98+ corners .add ((at_x_max , at_y_max ))
99+ return len (corners ) == 4
100+
101+
81102class PlotlyRenderer (Renderer ):
82103 """A renderer class inheriting from base for rendering mpl plots in plotly.
83104
@@ -705,6 +726,19 @@ def _draw_axes_line(self, props):
705726 self .msg += " Heck yeah, I drew that reference line\n "
706727 return True
707728
729+ def _is_axes_span (self , props ):
730+ """Check whether a path qualifies as a reference span (e.g. axhspan,
731+ axvspan): an axis-aligned rectangle in a blended axes/data
732+ transform."""
733+ if not isinstance (props ["mplobj" ], (mpatches .Rectangle , mpatches .Polygon )):
734+ return False
735+ if not (
736+ props ["coordinates" ] == "display"
737+ and _has_blended_transform (props ["mplobj" ].get_transform ())
738+ ):
739+ return False
740+ return _is_axis_aligned_rectangle (props ["data" ])
741+
708742 def _draw_axes_span (self , props ):
709743 """Draw a reference span (axhspan/axvspan) as a layout shape.
710744
@@ -977,11 +1011,7 @@ def draw_path(self, **props):
9771011 elif isinstance (props ["mplobj" ], mpatches .StepPatch ):
9781012 self .msg += " Drawing a step path\n "
9791013 self ._draw_step_path (props )
980- elif (
981- isinstance (props ["mplobj" ], (mpatches .Rectangle , mpatches .Polygon ))
982- and props ["coordinates" ] == "display"
983- and _has_blended_transform (props ["mplobj" ].get_transform ())
984- ):
1014+ elif self ._is_axes_span (props ):
9851015 if self ._processing_legend :
9861016 self .msg += " Using native legend\n "
9871017 else :
0 commit comments