3
votes

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.

4
If the code is the same, then there should be a difference in any package or in the python version. Use pip freeze both in the global and in the virtual environment and compare. Also see if the python version itself is the same. It could also be that matplotlib uses different backends, so also check matplotlib.get_backend().swenzel
the python version is the same. the matplotlib was different. I uninstalled it from my virtualenv and installed the same version found in my global python. However, the problem persists. @swenzelMAS
How about the backend?swenzel
I get MacOSX for both @swenzel.MAS
a possible thing to try is to use an alternative backend matplotlib.org/faq/usage_faq.html#what-is-a-backend for matplotlib (e.g. use qt4 instead of 'macosx')J Richard Snape

4 Answers

17
votes

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
matplotlib.use('qt4agg')

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.

0
votes

What finally worked for me was to make a local matplotlibrc file containing the directive: backend: TkAgg.

# Within working directory where running python
vim matplotlibrc
# new file via vim, Nano, whatever
backend: TkAgg

This was useful:

import matplotlib
print matplotlib.rcParams['backend']

Other Notes:

I had also installed pyqt using Homebrew (brew install pyqt) and copied (could just move it) into $MyVirtEnv/lib/python2.7/site-packages/ from /usr/local/lib/python2.7/site-packages/PyQt4

But when I

import matplotlib
matplotlib.use('qt4Agg')
import librosa
 "Matplotlib qt-based backends require an external PyQt4, PyQt5,\n"
ImportError: Matplotlib qt-based backends require an external PyQt4,   PyQt5,
or PySide package to be installed, but it was not found.

Had also tried

pip install pyside
Successfully installed pyside-1.2.4

Same error

>>> import pyside
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named pyside

Also got the following error at some point when trying to reimport matplotlib (i think).

from . import cache
ImportError: cannot import name cache
0
votes

As pointed out in Matplotlib virtualenv FAQ, vext is the solution here.

It allows to use the system-wide pyqt5 for instance.

This is necessary until PyQt5 setup knows about virtualenvs:

pip3 install vext.pyqt5
0
votes

Just add plt.show() at the end and the problem was solved for me.