mr_js
mr_js

Reputation: 1029

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.

Upvotes: 8

Views: 3036

Answers (1)

tylerthemiler
tylerthemiler

Reputation: 5746

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))

Upvotes: 4

Related Questions