MAS MAS - 1 year ago 51
Python Question

matplotlib example code not working on python virtual environment

I am trying to display the x y z coordinate of an image in matplotlib. the example code work perfectly well on the global python installation: As I move the cursor the x,y,z values get updated instantaneously. However, when I run the example code on a python virtual environment, I would click on the images several times for the coordinate to show in the first place, then when I click on different positions it would update for some. After few clicks, the coordinates will no longer update.

I don't know how to debug this.

Answer Source

This is likely to be a problem with the macosx backend for matplotlib. Switch to using an alternative backend for matplotlib (e.g. use qt4 instead of 'macosx'). For details of how to switch backend and what exactly that means - see the docs here. Note that you might have to install the backend first - e.g. pyqt to use the qt4agg backend as I'm suggesting here.

In summary - the backend deals with the output from matplotlib and matplotlib can target different output formats. These can be gui display output formats (for instance wx, qt4 and so on), or file outputs (for instance pdf). These are known as interactive and non-interactive backends respectively.

To change backend either do

import matplotlib

in code, or - if you want to change for every time you start matplotlib - edit your matplotlibrc file setting the backend attribute e.g.

backend: Qt4Agg

N.B. I was alerted by a comment that since posting this answer, matplotlib docs now refer to this issue and suggest a workaround, although the commenter noted that the solution offered in this answer (switch to Qt backend) worked for them where the official docs workaround was not possible for them.