From 61d4f69f457d5f6abecf444263f7b615ac9729af Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 20 Jan 2022 15:44:36 -0500 Subject: [PATCH] Backport PR #22254: Disable QuadMesh cursor data by default --- doc/api/next_api_changes/behavior/22254-DS.rst | 5 +++++ lib/matplotlib/collections.py | 17 +++++++++++++++++ lib/matplotlib/tests/test_collections.py | 15 +++++++++++---- 3 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 doc/api/next_api_changes/behavior/22254-DS.rst diff --git a/doc/api/next_api_changes/behavior/22254-DS.rst b/doc/api/next_api_changes/behavior/22254-DS.rst new file mode 100644 index 000000000000..83e53ae60477 --- /dev/null +++ b/doc/api/next_api_changes/behavior/22254-DS.rst @@ -0,0 +1,5 @@ +QuadMesh cursor data disabled by default +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Showing the cursor data of a `.QuadMesh` is now disabled by default, as it has +significant performance issues with large meshes. To manually enable this +use :meth:`.QuadMesh.set_show_cursor_data`. diff --git a/lib/matplotlib/collections.py b/lib/matplotlib/collections.py index 1a20e66e0239..e7e630475e0d 100644 --- a/lib/matplotlib/collections.py +++ b/lib/matplotlib/collections.py @@ -2011,6 +2011,7 @@ def __init__(self, *args, **kwargs): self._shading = shading self._bbox = transforms.Bbox.unit() self._bbox.update_from_data_xy(self._coordinates.reshape(-1, 2)) + self._show_cursor_data = False # super init delayed after own init because array kwarg requires # self._coordinates and self._shading super().__init__(**kwargs) @@ -2207,7 +2208,23 @@ def draw(self, renderer): renderer.close_group(self.__class__.__name__) self.stale = False + def set_show_cursor_data(self, show_cursor_data): + """ + Set whether cursor data should be shown. + + Notes + ----- + This is set to `False` by default for new quad meshes. Showing cursor + data can have significant performance impacts for large meshes. + """ + self._show_cursor_data = show_cursor_data + + def get_show_cursor_data(self): + return self._show_cursor_data + def get_cursor_data(self, event): + if not self._show_cursor_data: + return contained, info = self.contains(event) if len(info["ind"]) == 1: ind, = info["ind"] diff --git a/lib/matplotlib/tests/test_collections.py b/lib/matplotlib/tests/test_collections.py index bc837f8db51d..fb7dade2b459 100644 --- a/lib/matplotlib/tests/test_collections.py +++ b/lib/matplotlib/tests/test_collections.py @@ -1032,13 +1032,20 @@ def test_quadmesh_cursor_data(): fig, ax = plt.subplots() *_, qm = ax.hist2d( np.arange(11)**2, 100 + np.arange(11)**2) # width-10 bins + x, y = ax.transData.transform([1, 101]) event = MouseEvent('motion_notify_event', fig.canvas, x, y) + + assert qm.get_show_cursor_data() is False + assert qm.get_cursor_data(event) is None + + qm.set_show_cursor_data(True) assert qm.get_cursor_data(event) == 4 # (0**2, 1**2, 2**2, 3**2) - for out_xydata in []: - x, y = ax.transData.transform([-1, 101]) - event = MouseEvent('motion_notify_event', fig.canvas, x, y) - assert qm.get_cursor_data(event) is None + + # Outside the quadmesh bounds + x, y = ax.transData.transform([-1, 101]) + event = MouseEvent('motion_notify_event', fig.canvas, x, y) + assert qm.get_cursor_data(event) is None def test_get_segments():