3

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.

MAS
  • 2,665
  • 7
  • 26
  • 49
  • 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 Oct 13 '15 at 11:13
  • 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. @swenzel – MAS Oct 13 '15 at 11:47
  • 1
    How about the backend? – swenzel Oct 13 '15 at 11:53
  • I get MacOSX for both @swenzel. – MAS Oct 14 '15 at 02:31
  • the below link seems to have a similar problem and describe the problem better. However, It doesn't have an answer. http://stackoverflow.com/questions/29754360/matplotlib-key-press-event-ignored-in-virtualenv?rq=1 – MAS Oct 14 '15 at 02:41
  • 1
    a possible thing to try is to use an alternative backend http://matplotlib.org/faq/usage_faq.html#what-is-a-backend for matplotlib (e.g. use qt4 instead of 'macosx') – J Richard Snape Oct 15 '15 at 20:50
  • Thanks a lot. This solved the problem. @JRichardSnape – MAS Oct 16 '15 at 05:52
  • @MAS great - I've added it as an answer so you can accept and leave the info here for any future visitors – J Richard Snape Oct 16 '15 at 22:58

4 Answers4

17

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.

J Richard Snape
  • 18,946
  • 5
  • 47
  • 73
  • Worked for me (after installing pyqt with brew)! The matplotlib documentation suggests much longer workarounds, that I could not actually make in practice.. http://matplotlib.org/faq/virtualenv_faq.html – SeF Feb 06 '16 at 17:27
0

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
Community
  • 1
  • 1
MikeiLL
  • 5,393
  • 3
  • 30
  • 56
0

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
ederag
  • 2,029
  • 20
  • 44
0

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

Man
  • 1
  • 1
    This is practically a code-only answer. Would you like to help fighting the misconception that StackOverflow is a free code writing service, by adding an explanation? – Yunnosch Aug 20 '19 at 17:13