|
| 1 | +""" |
| 2 | +============ |
| 3 | +Mouse Cursor |
| 4 | +============ |
| 5 | +
|
| 6 | +This example sets an alternative cursor on a figure canvas. |
| 7 | +
|
| 8 | +Note, this is an interactive example, and must be run to see the effect. |
| 9 | +""" |
| 10 | + |
| 11 | +import matplotlib.pyplot as plt |
| 12 | +from matplotlib.backend_tools import Cursors |
| 13 | + |
| 14 | + |
| 15 | +fig, axs = plt.subplots(len(Cursors), figsize=(6, len(Cursors) + 0.5), |
| 16 | + gridspec_kw={'hspace': 0}) |
| 17 | +fig.suptitle('Hover over an Axes to see alternate Cursors') |
| 18 | + |
| 19 | +for cursor, ax in zip(Cursors, axs): |
| 20 | + ax.cursor_to_use = cursor |
| 21 | + ax.text(0.5, 0.5, cursor.name, |
| 22 | + horizontalalignment='center', verticalalignment='center') |
| 23 | + ax.set(xticks=[], yticks=[]) |
| 24 | + |
| 25 | + |
| 26 | +def hover(event): |
| 27 | + if fig.canvas.widgetlock.locked(): |
| 28 | + # Don't do anything if the zoom/pan tools have been enabled. |
| 29 | + return |
| 30 | + |
| 31 | + fig.canvas.set_cursor( |
| 32 | + event.inaxes.cursor_to_use if event.inaxes else Cursors.POINTER) |
| 33 | + |
| 34 | + |
| 35 | +fig.canvas.mpl_connect('motion_notify_event', hover) |
| 36 | + |
| 37 | +plt.show() |
| 38 | + |
| 39 | +############################################################################# |
| 40 | +# |
| 41 | +# .. admonition:: References |
| 42 | +# |
| 43 | +# The use of the following functions, methods, classes and modules is shown |
| 44 | +# in this example: |
| 45 | +# |
| 46 | +# - `matplotlib.backend_bases.FigureCanvasBase.set_cursor` |
0 commit comments