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