2828import matplotlib .pyplot as plt
2929import numpy as np
3030
31+ from matplotlib .backend_bases import MouseEvent
32+
3133
3234class Cursor :
3335 """
@@ -71,6 +73,11 @@ def on_mouse_move(self, event):
7173cursor = Cursor (ax )
7274fig .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+
8896class BlittedCursor :
8997 """
9098 A cross-hair cursor using blitting for faster redraw.
@@ -152,6 +160,11 @@ def on_mouse_move(self, event):
152160blitted_cursor = BlittedCursor (ax )
153161fig .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+
168182class 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):
218232line , = ax .plot (x , y , 'o' )
219233snap_cursor = SnappingCursor (ax , line )
220234fig .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+
221242plt .show ()
0 commit comments