diff --git a/lib/matplotlib/tests/test_widgets.py b/lib/matplotlib/tests/test_widgets.py index 0ac24dc8e8e4..a35e310c2962 100644 --- a/lib/matplotlib/tests/test_widgets.py +++ b/lib/matplotlib/tests/test_widgets.py @@ -1817,3 +1817,19 @@ def test_parent_axes_removal(): evt = DrawEvent('draw_event', fig.canvas, renderer) radio._clear(evt) checks._clear(evt) + + +def test_cursor_overlapping_axes_blitting_warning(): + """Test that a warning is raised and useblit is disabled for overlapping axes.""" + fig = plt.figure() + ax1 = fig.add_axes([0.1, 0.1, 0.8, 0.8]) + ax2 = fig.add_axes([0.2, 0.2, 0.6, 0.6]) # Explicitly overlaps ax1 + + match_text = ( + "Cursor blitting is currently not supported on " + "overlapping axes" + ) + with pytest.warns(UserWarning, match=match_text): + cursor = widgets.Cursor(ax1, useblit=True) + + assert cursor.useblit is False diff --git a/lib/matplotlib/widgets.py b/lib/matplotlib/widgets.py index 92c9b779ff09..9ab4548157fd 100644 --- a/lib/matplotlib/widgets.py +++ b/lib/matplotlib/widgets.py @@ -2084,6 +2084,16 @@ def __init__(self, ax, *, horizOn=True, vertOn=True, useblit=False, self.vertOn = vertOn self.useblit = useblit and self.canvas.supports_blit # TODO: make dynamic + if self.useblit: + for ax_ in ax.get_figure(root=True).get_axes(): + if ax_ is not ax and ax.bbox.overlaps(ax_.bbox): + _api.warn_external( + "Cursor blitting is currently not supported on " + "overlapping axes; falling back to useblit=False." + ) + self.useblit = False + break + if self.useblit: lineprops['animated'] = True self.lineh = ax.axhline(ax.get_ybound()[0], visible=False, **lineprops)