FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[ENH]: Remove `canvas.draw` from `widgets.Cursor.onmove` · Issue #26901 · matplotlib/matplotlib · GitHub

Repository navigation

[ENH]: Remove canvas.draw from widgets.Cursor.onmove #26901

Closed

Description

Problem

A busy icon appears every time the mouse goes off the axes and the cursor is cleared.
For image files ~100MB, the busy icon lasts 1-2 seconds each time.

Sample code:

from matplotlib.widgets import Cursor
from matplotlib import pyplot as plt
from matplotlib import cm
from PIL import Image

fig, ax = plt.subplots()

img = Image.open(r"Path to large image file ~100MB")
ax.imshow(img, cmap=cm.gray)

cursor = Cursor(ax, useblit=True, color='red', linewidth=1)

plt.show()

Proposed solution

I think the behavior of not clearing the cursor when the mouse goes off the axes is acceptable.
It is also consistent with the behavior of the similar class MultiCursor which doesn't call canvas.draw.

If accepted, the following lines can be removed and needclear will also be removed:

if self.needclear:
self.canvas.draw()
self.needclear = False

Activity

  1. jklymak commented on Sep 27, 2023

    Member

    @anntzer @oscargus either of you have any comments about this?

  2. oscargus commented on Sep 27, 2023

    Member

    I sort of agree that in general it may not be a problem if the cursor is not cleared. However, I was thinking that one possibly can add a keyword argument that can be used to control this so that we do not break any previous behavior, but allowing users to avoid clearing it if it is beneficial/acceptable.

    Maybe clearoutside (or a better name), which defaults to True and then something like

    if self._clearoutside and self.needclear: 
         self.canvas.draw() 
         self.needclear = False
    

    or maybe even an early return before setting it to non-visible?

    (Edit: probably one would like to consider enabling clearing for MultiCursors...)

    (Edit 2: as needclear is, sort of, public, we would also need to go through the deprecation dance. Hence, probably easier to optionally disable it.)

  3. anntzer commented on Sep 28, 2023

    Contributor

    It seems clear that needclear should have been private, and should be made so via deprecation.
    I didn't really look in depth but why does Cursor even need to clear and redraw everything when the mouse goes off the axis?

  4. oscargus commented on Sep 28, 2023

    Member

    I didn't really look in depth but why does Cursor even need to clear and redraw everything when the mouse goes off the axis?

    It is quite nice once you have it. However not clear why it has to redraw everything and cannot rely on the normal drawing/blitting. Probably an easy, but not efficient, way to do it.

  5. komoto48g commented on Sep 29, 2023

    Author

    Thank you for your review.
    I added the following monkey patch to my program and it worked fine for me.

    if 1:
        class Cursor(Cursor):
            def onmove(self, event):
                if self.ignore(event)\
                  or not self.canvas.widgetlock.available(self):
                    return
                ## xdata, ydata = self._get_data_coords(event) # >= 3.8 only
                xdata, ydata = event.xdata, event.ydata
                self.linev.set_xdata((xdata, xdata))
                self.linev.set_visible(self.visible and self.vertOn)
                self.lineh.set_ydata((ydata, ydata))
                self.lineh.set_visible(self.visible and self.horizOn)
                if self.visible and (self.vertOn or self.horizOn):
                    self._update()

    Since events.xdata and ydata return None , it is simply cleared rather than drawn.
    (In the latest version, _get_data_coords would be better)

  6. added this to the v3.11.0 milestone on Apr 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL