K. Shores
K. Shores

Reputation: 1005

Ignore all mouse clicks on a matplotlib plot

I've recently built a python script that interacts with an Arduino and a piece of hardware that uses LIDAR to map out a room. Everything works great, but anytime you click on the plot that is generated with maptotlib, the computer freaks out and crashes the script that is running. This is partly because I was given a $300 computer to run this on, so it's not very powerful. However, I feel like even a $300 computer should be able to handle a mouse click.

How can I ignore mouse clicks entirely with matplotlib so that the computer doesn't freak out and crash the script?

If that's not the correct solution, what might be a better solution?

Edit: This is an interactive plotting session (sort of, I just replace the old data with the new data, there is no plot.ion() command called). So, I cannot just save the plot and show it. The Arduino transmits data constantly.

Upvotes: 0

Views: 336

Answers (2)

Ari Cooper-Davis
Ari Cooper-Davis

Reputation: 3485

I feel that this might be more easily resolved by altering the hardware - can you temporarily unplug the mouse, or tape over the track pad to stop people fiddling with it?

I suggest this because your crashing script will always process mouse-clicks in some way, and if you don't know what's causing the crashes then you may be better off just ensuring that there are no clicks.

Upvotes: 1

armatita
armatita

Reputation: 13465

You can try bypassing the click event on your plot:

import matplotlib.pyplot as plt

fig = plt.figure()

plt.axes(xlim = (0, 3), ylim = (0, 3))

def onclick(event):
    pass

fig.canvas.mpl_connect('button_press_event', onclick)

plt.show()

, but I doubt this will work. What I recommend as a solution (if the stuff above does not work) is to make your plot and save it to file (without showing it):

plt.savefig('fname.png')
plt.close()

Than make python open the image (using subprocess for example) with whatever external tool you prefer in your OS.

I'm saying this because I suspect you might have some kind of packages incompatibility that is causing your script to crash (maybe a backend from mpl or whatever other library you are using over mpl). If this is the case more information is needed to try solving the issue.

Upvotes: 1

Related Questions