Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a custom mouse cursor in Matplotlib

I am interested in creating a custom mouse cursor, so that during drag and pick events on certain lines or points, the mouse changes from an arrow to a hand (or other symbol).

What is the best method of doing this?

I assume this is possible since the mouse cursor changes to a small cross hair during zoom operations. If possible, a solution using the PyQt/PySide backend would be preferable.

like image 786
mr_js Avatar asked Oct 13 '11 14:10

mr_js


People also ask

How do I use a data cursor in Python?

Hitting “d” again will re-display all of the datacursors that were hidde. To disable or re-enable interactive datacursors, press “t” (for “toggle”). Pressing “t” will prevent clicks from creating datacursors until “t” is pressed again.


1 Answers

What you need is mpl_canvas. Follow this tutorial to set one up.

With an mpl_canvas, you can then set up events that get triggered.

fig = matplotlib.figure.Figure()
cid = fig.canvas.mpl_connect('button_press_event', your_method)

There are several kinds of signals under here (listed under Events).

With your signal set up, your_method gets called, with an event parameter. So do something like:

def your_method(event):
    print('Your x and y mouse positions are ', event.xdata, event.ydata)

Click on the corrosponding Class and description links to see what exactly is in event. for a specific mpl_canvas Event.

In your specific case, to change how the mouse looks your_method should look something like:

 def your_method(event):
     #changes cursor to +
     QtGui.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.CrossCursor))
like image 119
tylerthemiler Avatar answered Oct 06 '22 16:10

tylerthemiler