Skip to content

Cleanup cursor_demo. #12955

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions examples/misc/cursor_demo_sgskip.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
Cursor Demo
===========

This example shows how to use matplotlib to provide a data cursor. It
uses matplotlib to draw the cursor and may be a slow since this
requires redrawing the figure with every mouse move.
This example shows how to use Matplotlib to provide a data cursor. It uses
Matplotlib to draw the cursor and may be a slow since this requires redrawing
the figure with every mouse move.

Faster cursoring is possible using native GUI drawing, as in
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's also https://matplotlib.org/gallery/widgets/cursor.html which seems like a better builtin version of this example. It should at least be linked.

Given this, do we still need this example here? If so, it's probably only for educational puposes and that should be mentioned.

wxcursor_demo.py.
:doc:`/gallery/user_interfaces/wxcursor_demo_sgskip`.

The mpldatacursor and mplcursors third-party packages can be used to achieve a
similar effect. See
The mpldatacursor__ and mplcursors__ third-party packages can be used to
achieve a similar effect.

https://github.com/joferkington/mpldatacursor
https://github.com/anntzer/mplcursors
__ https://github.com/joferkington/mpldatacursor
__ https://github.com/anntzer/mplcursors
"""

import matplotlib.pyplot as plt
import numpy as np

Expand All @@ -39,13 +40,13 @@ def mouse_move(self, event):
self.ly.set_xdata(x)

self.txt.set_text('x=%1.2f, y=%1.2f' % (x, y))
plt.draw()
self.ax.figure.canvas.draw()


class SnaptoCursor(object):
"""
Like Cursor but the crosshair snaps to the nearest x,y point
For simplicity, I'm assuming x is sorted
Like Cursor but the crosshair snaps to the nearest x, y point.
For simplicity, this assumes that *x* is sorted.
"""

def __init__(self, ax, x, y):
Expand All @@ -58,13 +59,11 @@ def __init__(self, ax, x, y):
self.txt = ax.text(0.7, 0.9, '', transform=ax.transAxes)

def mouse_move(self, event):

if not event.inaxes:
return

x, y = event.xdata, event.ydata

indx = min(np.searchsorted(self.x, [x])[0], len(self.x) - 1)
indx = min(np.searchsorted(self.x, x), len(self.x) - 1)
x = self.x[indx]
y = self.y[indx]
# update the line positions
Expand All @@ -73,16 +72,20 @@ def mouse_move(self, event):

self.txt.set_text('x=%1.2f, y=%1.2f' % (x, y))
print('x=%1.2f, y=%1.2f' % (x, y))
plt.draw()
self.ax.figure.canvas.draw()


t = np.arange(0.0, 1.0, 0.01)
s = np.sin(2 * 2 * np.pi * t)
fig, ax = plt.subplots()

# cursor = Cursor(ax)
cursor = SnaptoCursor(ax, t, s)
plt.connect('motion_notify_event', cursor.mouse_move)
fig, ax = plt.subplots()
ax.plot(t, s, 'o')
cursor = Cursor(ax)
fig.canvas.mpl_connect('motion_notify_event', cursor.mouse_move)

fig, ax = plt.subplots()
ax.plot(t, s, 'o')
plt.axis([0, 1, -1, 1])
snap_cursor = SnaptoCursor(ax, t, s)
fig.canvas.mpl_connect('motion_notify_event', snap_cursor.mouse_move)

plt.show()
2 changes: 1 addition & 1 deletion examples/user_interfaces/wxcursor_demo_sgskip.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
Example to draw a cursor and report the data coords in wx.
"""


from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
from matplotlib.figure import Figure
Expand Down Expand Up @@ -63,6 +62,7 @@ def OnInit(self):
frame.Show(True)
return True


if __name__ == '__main__':
app = App(0)
app.MainLoop()