Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7d5c8a5

Browse files
committed
Add simulated mouse events to cursor demos
So that the cursors show up on online docs
1 parent 1965b09 commit 7d5c8a5

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

examples/event_handling/cursor_demo.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import matplotlib.pyplot as plt
2929
import numpy as np
3030

31+
from matplotlib.backend_bases import MouseEvent
32+
3133

3234
class Cursor:
3335
"""
@@ -71,6 +73,11 @@ def on_mouse_move(self, event):
7173
cursor = Cursor(ax)
7274
fig.canvas.mpl_connect('motion_notify_event', cursor.on_mouse_move)
7375

76+
# Simulate a mouse move to (0.5, 0.5), needed for online docs
77+
t = ax.transData
78+
MouseEvent(
79+
"motion_notify_event", ax.figure.canvas, *t.transform((0.5, 0.5))
80+
)._process()
7481

7582
# %%
7683
# Faster redrawing using blitting
@@ -85,6 +92,7 @@ def on_mouse_move(self, event):
8592
# created whenever the figure changes. This is achieved by connecting to the
8693
# ``'draw_event'``.
8794

95+
8896
class BlittedCursor:
8997
"""
9098
A cross-hair cursor using blitting for faster redraw.
@@ -152,6 +160,11 @@ def on_mouse_move(self, event):
152160
blitted_cursor = BlittedCursor(ax)
153161
fig.canvas.mpl_connect('motion_notify_event', blitted_cursor.on_mouse_move)
154162

163+
# Simulate a mouse move to (0.5, 0.5), needed for online docs
164+
t = ax.transData
165+
MouseEvent(
166+
"motion_notify_event", ax.figure.canvas, *t.transform((0.5, 0.5))
167+
)._process()
155168

156169
# %%
157170
# Snapping to data points
@@ -165,6 +178,7 @@ def on_mouse_move(self, event):
165178
# the lag due to many redraws. Of course, blitting could still be added on top
166179
# for additional speedup.
167180

181+
168182
class SnappingCursor:
169183
"""
170184
A cross-hair cursor that snaps to the data point of a line, which is
@@ -218,4 +232,11 @@ def on_mouse_move(self, event):
218232
line, = ax.plot(x, y, 'o')
219233
snap_cursor = SnappingCursor(ax, line)
220234
fig.canvas.mpl_connect('motion_notify_event', snap_cursor.on_mouse_move)
235+
236+
# Simulate a mouse move to (0.5, 0.5), needed for online docs
237+
t = ax.transData
238+
MouseEvent(
239+
"motion_notify_event", ax.figure.canvas, *t.transform((0.5, 0.5))
240+
)._process()
241+
221242
plt.show()

examples/widgets/annotated_cursor.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import numpy as np
2525
import matplotlib.pyplot as plt
2626

27+
from matplotlib.backend_bases import MouseEvent
28+
2729

2830
class AnnotatedCursor(Cursor):
2931
"""
@@ -312,6 +314,12 @@ def _update(self):
312314
color='red',
313315
linewidth=2)
314316

317+
# Simulate a mouse move to (-2, 10), needed for online docs
318+
t = ax.transData
319+
MouseEvent(
320+
"motion_notify_event", ax.figure.canvas, *t.transform((-2, 10))
321+
)._process()
322+
315323
plt.show()
316324

317325
# %%
@@ -339,4 +347,10 @@ def _update(self):
339347
useblit=True,
340348
color='red', linewidth=2)
341349

350+
# Simulate a mouse move to (-2, 10), needed for online docs
351+
t = ax.transData
352+
MouseEvent(
353+
"motion_notify_event", ax.figure.canvas, *t.transform((-2, 10))
354+
)._process()
355+
342356
plt.show()

0 commit comments

Comments
 (0)