16

I have pip installed matplotlib in my virtualenv and I am trying to plot a simple graph. I use Eclipse and PyDev. When I run the script from Eclipse it doesn't display any graph at all. I have tried the suggestions proposed in other questions such as adding plt.ion() but that doesn't work either. I have also tried the same code in the console and again nothing. Is this a problem with my configuration? If so how can I fix it?

The failing code is:

    import matplotlib.pyplot as plt
    radius = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
    area = [3.14159, 12.56636, 28.27431, 50.26544, 78.53975, 113.09724]
    plt.plot(radius, area)
    plt.show()

The code is not the problem. It works just fine out of virtualenv. The problem is when I use it in virtualenv. The problem is somehow related to my configuration. And just to clear things up I DON'T GET ANY error messages. It's just that the window wouldn't show up.

Thanks

George Eracleous
  • 3,981
  • 5
  • 34
  • 49
  • you could start showing the code that fails – joaquin Jan 29 '12 at 19:37
  • 1
    thanks for the edit. As far as I can understand now, your question is somehow misleading as it seems to indicate that the problem is (also) pydev. If you can discard eclipse-pydev as the problem (maybe running on the console or idle instead of pydev), you should modify your title and tags to change pydev with virtualenv. This would help the right people to look at the question. – joaquin Jan 29 '12 at 21:03
  • How it failed? any error messages? – Tyler Long Feb 01 '12 at 10:26
  • No error at all. The script runs just fine. But it doesn't render a window with the graph. WHen I use the console outside the virtualenv everything is fine. But when I am in virtualenv there's no window – George Eracleous Feb 01 '12 at 10:48
  • 1
    Have you tried the [`--system-site-packages`](http://www.virtualenv.org/en/latest/#the-system-site-packages-option) option? – Tianyang Li Jan 14 '13 at 14:18
  • 1
    You get such behavior if you're using the `Agg` backend, which can only kick out images and not generate windows. `plt.show()` will be silent as you observe, but you can force the warning if you `plt.figure().show()` : `UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure` – Nick T Apr 09 '13 at 20:42

5 Answers5

9

I had the same issue, and installing matplotlib using easy_install instead of pip did not solve it. In the end, I found out that the problem was simply that matplotlib could not find any backend for plotting.

I solved it by doing the following (I am using Debian wheezy):

pip uninstall matplotlib
sudo apt-get install tcl-dev tk-dev
pip install matplotlib
Wookai
  • 18,747
  • 16
  • 70
  • 85
9

Your code works inside my virtualenv on OSX 10.7 with Python 2.7:

enter image description here

What version of Python are you using inside your virtualenv? My guess is that either you have not installed a matplotlib dependency or your installation of an installed dependency was not properly performed. On Python 2.7 here is what I did to install matplotlib. Can you try these steps in a new virtualenv and see if it works for you?

pip install numpy
pip install scipy
easy_install matplotlib
drbunsen
  • 8,781
  • 20
  • 62
  • 91
6

First off, you might want to check out:

http://matplotlib.org/faq/installing_faq.html#matplotlib-compiled-fine-but-nothing-shows-up-when-i-use-it

To see what's going wrong, check out the matplotlib using git instead of either pip or easy_install. We're going to do a more manual install:

git clone git@github.com:matplotlib/matplotlib.git
cd matplotlib
python setup.py

This will print out the configuration of what pip or easy_install would have done. Look through the "OPTIONAL BACKEND DEPENDENCIES" and make sure that some of the ones that produce windows are enabled (Tkinter, Gtk+, Mac OS X native, Qt, Cairo, etc.). If you see that none of these are available, then you need to install some of these libraries for your operating system before you continue installing matplotlib.

After installing say, Tk (on ubuntu: sudo apt-get install tcl-dev tk-dev), then when you re-run

python setup.py

you will see that the Tk backend is enabled. Proceeding with

python setup.py build && python setup.py install

should get you up and running... but at that point you might even just delete the whole git clone directory and go back to installing with pip.

  • It worked for me. I installed with pip and not with easy_install. – jgomo3 Nov 27 '12 at 21:19
  • In my environment, magically, the global matplotlib package was using the TkAgg just fine, but neither `tcl-dev` or `tk-dev` were installed. Resolving that fixed the problem. Using `easy_install` vs. `pip` did nothing. However, before installing the Tk/Tcl packages, my virtualenv setup was still able to use the Tk python module and draw windows... – Nick T Apr 09 '13 at 21:09
  • Problems with tcl inside virtualenvs are documented but don't seem to be fixed yet. See [this ticket for virtualenv](https://github.com/pypa/virtualenv/issues/93). The suggestion of modifying `activate.bat` to set the path to the TCL files in the base python install fixed it for me. – turtlemonvh Aug 05 '13 at 21:45
4

If you have a version of matplotlib installed on your system version of python, then you can link to the system version of matplotlib. On my machine I did the following:

cd $VIRTUAL_ENV/lib/python2.7/site-packages
ln -s /usr/lib/pymodules/python2.7/matplotlib .
ln -s /usr/lib/pymodules/python2.7/matplotlib-1.1.1rc .

This avoids many of the problems with getting matplotlib to work in the virtualenv but limits you to using the system version of matplotlib (which on this machine is not too bad).

This method also allows you to use the --no-site-packages, but still have matplotlib work for you.

SillyBear
  • 125
  • 4
2

I am using Ubuntu 12.04 and Python 2.7.3 on my computer and when I use the matplotlib 1.2.0 in my virtualenv, the show() did not work until I upgrade it to the 1.2.1... All the bugs I had previous went away...

By doing this you will install all the dependencies of the matplotlib:

sudo apt-get build-dep python-matplotlib

To get the latest version of the matplotlib you can use:

pip install matplotlib

or upgrade it:

pip install matplotlib --upgrade