19

I'm using Python 2.7.3 in 64-bit. I installed pandas as well as matplotlib 1.1.1, both for 64-bit. Right now, none of my plots are showing. After attempting to plot from several different dataframes, I gave up in frustration and tried the following first example from http://pandas.pydata.org/pandas-docs/dev/visualization.html:

INPUT:

import matplotlib.pyplot as plt
ts = Series(randn(1000), index=date_range ('1/1/2000', periods=1000))
ts = ts.cumsum()
ts.plot()
pylab.show()

OUTPUT:

Axes(0.125,0.1;0.775x0.8)

And no plot window appeared. Other StackOverflow threads I've read suggested I might be missing DLLs. Any suggestions?

Nikhil
  • 16,194
  • 20
  • 64
  • 81
user1518837
  • 301
  • 1
  • 2
  • 5

3 Answers3

35

I'm not convinced this is a pandas issue at all.

Does

import matplotlib.pyplot as plt
plt.plot(range(10))
plt.show()

bring up a plot?

If not:

How did you install matplotlib? Was it from source or did you install it from a package manager/pre-built binary?

I suspect that if you run:

import matplotlib            
print matplotlib.rcParams['backend']

The result will be a non-GUI backend (almost certainly "Agg"). This suggests you don't have a suitable GUI toolkit available (I personally use Tkinter which means my backend is reported as "TkAgg").

The solution to this depends on your operating system, but if you can install a GUI library (one of Tkinter, GTK, QT4, PySide, Wx) then pyplot.show() should hopefully pop up a window for you.

HTH,

pelson
  • 21,252
  • 4
  • 92
  • 99
  • 1
    It turns out it does work and I was just using the incorrect syntax in my code. Thank you. – user1518837 Nov 02 '12 at 20:15
  • @pelson,did you install matplotlib with pip?because it works only for me if i use sudo command only – Eliethesaiyan Jun 03 '15 at 08:26
  • That is because your python is owned by root. That is the package manager pattern for Linux OSes. Take a look at http://stackoverflow.com/questions/7465445/how-to-install-python-modules-without-root-access for possible solutions. – pelson Jun 05 '15 at 13:14
  • 2
    I have a similar issue - nothing shows up when I plt.show(). I use python 3 and have installed matplotlib with pip3. rcParams['backend'] = 'Agg'. I installed python-tk via apt-get but still cannot get a window popping open. – disruptive Dec 08 '15 at 18:24
  • 1
    Try rcParams['backend'] = 'TkAgg' – pelson Jan 04 '16 at 14:37
1

I had this problem when working from within a virtualenv.

Cause

The cause of the problem is that when you pip install matplotlib, it fails to find any backends (even if they are installed on your machine), so it uses the "agg" backend, which does not make any plots, just writes files. To confirm that this is the case, go: python -c "import matplotlib; print matplotlib.get_backend()", you probably see agg.

I could however, successfully use matplotlib on the system (outside the virtualenv). I also failed to install PySide, PyQt, or get it to work for TkAgg, for various different reasons.

Solution

I eventually just made a link to my system version of matplotlib (starting from outside the venv):

...$ pip install matplotlib
...$ cd /to/my/venv/directory
...$ source venv/bin/activate
(venv) .... $ pip uninstall matplotlib
(venv) .... $ ln -s /usr/lib/pymodules/python2.7/matplotlib $VIRTUAL_ENV/lib.python*/site-packages

After that, I could use matplotlib and the plots would show. Your local version of matplotlib may be in a different place. To see where it is, go (outside of the venv, in python)

...$ python -c 'import matplotlib; matplotlib.__file__'
Peter
  • 12,274
  • 9
  • 71
  • 86
0

Try installing these libraries, it works for me:

$ sudo apt-get install tcl-dev tk-dev python-tk python3-tk
Alper Kucukkomurler
  • 1,706
  • 2
  • 13
  • 19