185

Help required on matplotlib. Yes, I did not forget calling the pyplot.show().

$ ipython --pylab

import matplotlib.pyplot as p 
p.plot(range(20), range(20))

It returns matplotlib.lines.Line2D at 0xade2b2c as the output.

p.show()

There is nothing to happen. No error message. No new window. Nothing. I install matplotlib by using pip and I didn't take any error messages.

Details:

I use,

  • Ubuntu
  • IPython v0.11
  • Python v2.6.6
  • matplotlib v1.0.1
Praveen Kumar K S
  • 2,660
  • 1
  • 17
  • 27
Baskaya
  • 6,602
  • 6
  • 25
  • 26

14 Answers14

183

If I set my backend to template in ~/.matplotlib/matplotlibrc, then I can reproduce your symptoms:

~/.matplotlib/matplotlibrc:

# backend      : GtkAgg
backend      : template

Note that the file matplotlibrc may not be in directory ~/.matplotlib/. In this case, the following code shows where it is:

>>> import matplotlib
>>> matplotlib.matplotlib_fname()

In [1]: import matplotlib.pyplot as p

In [2]: p.plot(range(20),range(20))
Out[2]: [<matplotlib.lines.Line2D object at 0xa64932c>]

In [3]: p.show()

If you edit ~/.matplotlib/matplotlibrc and change the backend to something like GtkAgg, you should see a plot. You can list all the backends available on your machine with

import matplotlib.rcsetup as rcsetup
print(rcsetup.all_backends)

It should return a list like:

['GTK', 'GTKAgg', 'GTKCairo', 'FltkAgg', 'MacOSX', 'QtAgg', 'Qt4Agg',
'TkAgg', 'WX', 'WXAgg', 'CocoaAgg', 'agg', 'cairo', 'emf', 'gdk', 'pdf',
'ps', 'svg', 'template']

Reference:

unutbu
  • 711,858
  • 148
  • 1,594
  • 1,547
  • 2
    I tried but I got an error something like: `ImportError: No module named _backend_gdk` – Baskaya Sep 30 '11 at 18:15
  • 6
    Sorry for lots of comment. It works. I tried Qt4Agg and hit. Thanks :).. Last thing, where can I find default matplotlibrc or is there any way to create it? – Baskaya Sep 30 '11 at 18:17
  • Sorry for untidiness. I think I [found](http://matplotlib.sourceforge.net/users/customizing.html) – Baskaya Sep 30 '11 at 18:26
  • More information on matplotlibrc is at http://matplotlib.sourceforge.net/users/customizing.html – Jim Garrison Aug 30 '12 at 08:12
  • @Thorn `locate matplotlibrc` – Ben Usman Nov 06 '13 at 15:08
  • 8
    This answer is old, config should now be in `~/.config/matplotlib/matplotlibrc` (for python 3, at least). I just had a related problem, and I think it was caused by using matplotlib in python 2.7, which created a `~/.matplotlib/` directory, and stopped python 3 from reading the config in `~/.config/matplotlib/`. Deleting `~/.matplotlib/` fixed the problem for me. – naught101 Nov 11 '13 at 23:50
  • I currently have a problem where 'Qt4Agg' does not work, but WXAgg, TkAgg, GTKAgg and GTK3Agg work. Neither plt.draw nor plt.show give a reponse for Gt4Agg, - until I make a KeyboardInterrupt and the window is very briefly shown. This is with a Ubuntu 12.04 and matplotlib pip install. – Finn Årup Nielsen May 22 '14 at 17:25
  • Here a great _how to_ http://www.pyimagesearch.com/2015/08/24/resolved-matplotlib-figures-not-showing-up-or-displaying/ – floatingpurr May 05 '16 at 13:22
72

I ran into the exact same problem on Ubuntu 12.04, because I installed matplotlib (within a virtualenv) using

pip install matplotlib

To make long story short, my advice is: don't try to install matplotlib using pip or by hand; let a real package manager (e.g. apt-get / synaptic) install it and all its dependencies for you.

Unfortunately, matplotlib's backends (alternative methods for actually rendering your plots) have all sorts of dependencies that pip will not deal with. Even worse, it fails silently; that is, pip install matplotlib appears to install matplotlib successfully. But when you try to use it (e.g. pyplot.show()), no plot window will appear. I tried all the different backends that people on the web suggest (Qt4Agg, GTK, etc.), and they all failed (i.e. when I tried to import matplotlib.pyplot, I get ImportError because it's trying to import some dependency that's missing). I then researched how to install those dependencies, but it just made me want to give up using pip (within virtualenv) as a viable installation solution for any package that has non-Python package dependencies.

The whole experience sent me crawling back to apt-get / synaptic (i.e. the Ubuntu package manager) to install software like matplotlib. That worked perfectly. Of course, that means you can only install into your system directories, no virtualenv goodness, and you are stuck with the versions that Ubuntu distributes, which may be way behind the current version...

Christopher Lee
  • 821
  • 6
  • 3
  • 5
    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:19
  • 1
    %Christopher Lee The problem with figures not showing without any error/other complaint is because the default backend when you `pip install` is `agg`. I'm not sure why, but you can change it as per the unutbu's answer. `pip` has the major advantage of installing an up-to-date version - `apt` installs an outdated version. Sometimes there are bug fixes/new features that you want/need. – drevicko Feb 25 '13 at 22:34
  • agree with @drevicko `pip` is usually a great solution to install python packages. – RNA Oct 15 '13 at 21:20
  • 2
    Just deactivating `virtualenv` worked! And I was able to install matplotlib 1.4 from the source systemwide (on Ubuntu), but not under `virtualenv`. – Sergey Orshanskiy Sep 05 '14 at 00:45
  • Great! Your answer really helped! "pip uninstall ...", "apt-get install ..." did the trick! – Alexey May 09 '16 at 07:21
  • Okay, so what's the solution for virtualenv then? Outdated packages from package manager or latest but useless install from pip? – Sebastialonso Jan 19 '17 at 15:25
  • 1
    And what's the solution for those of us stuck on Windows work machines who can't use apt-get? – ale19 Feb 24 '17 at 15:30
  • The main reason for the down-vote: "don't try to install matplotlib using pip or by hand; let a real package manager (e.g. apt-get / synaptic) install it and all its dependencies for you". – gmagno Mar 05 '19 at 00:51
36

%matplotlib inline

For me working with notebook, adding the above line before the plot works.

grampus
  • 361
  • 3
  • 3
10

For future reference,

I have encountered the same problem -- pylab was not showing under ipython. The problem was fixed by changing ipython's config file {ipython_config.py}. In the config file

c.InteractiveShellApp.pylab = 'auto'

I changed 'auto' to 'qt' and now I see graphs

anon
  • 101
  • 1
  • 2
  • 5
    or using "ipython --pylab=qt" – andrew cooke Nov 30 '12 at 13:44
  • This will work, but it's only a partial solution: If your default backend is `agg`, I don't thing that `%pylab inline` will work, nor will plots show when you run scripts directly ie:`python myscript.py`. unutbu's answer should fix it everywhere. – drevicko Feb 25 '13 at 22:37
9

Just type:

plt.ion()

See https://www.youtube.com/watch?v=1zmV8lZsHF4 at 23:30 !

plt is used because of my import: import matplotlib.pyplot as plt

I'm using python2.7 on a mac with iTerm2.

StefanM
  • 789
  • 1
  • 10
  • 17
tnuts
  • 93
  • 1
  • 5
8

What solved my problem was just using the below two lines in ipython notebook at the top

%matplotib inline
%pylab inline

And it worked. I'm using Ubuntu16.04 and ipython-5.1

Bhanu Pratap Singh
  • 879
  • 10
  • 14
  • Done - Though [this blogpost](http://carreau.github.io/posts/10-No-PyLab-Thanks.html) bashes pylab but you will get an idea why adding ```%pylab inline``` makes it work. – Bhanu Pratap Singh Oct 01 '16 at 16:39
3

Adding the following two lines before importing pylab seems to work for me

import matplotlib
matplotlib.use("gtk")

import sys
import pylab
import numpy as np
Tunaki
  • 116,530
  • 39
  • 281
  • 370
Pau
  • 31
  • 2
2

I had to install matplotlib from source to get this to work. The key instructions (from http://www.pyimagesearch.com/2015/08/24/resolved-matplotlib-figures-not-showing-up-or-displaying/) are:

$ workon plotting
$ pip uninstall matplotlib
$ git clone https://github.com/matplotlib/matplotlib.git
$ cd matplotlib
$ python setup.py install

By changing the backend, as @unutbu says, I just ran into loads more problems with all the different backends not working either.

Rikki
  • 954
  • 13
  • 16
  • 1
    Worked a treat, only used the git clone and python setup steps but when i run it with plt.show() no issues. – Steve Jan 27 '17 at 15:32
  • I received a compiler error when trying to run `python setup.py install` :/ I wish I could see the flipping window! – user_loser May 01 '21 at 04:17
0

Be sure to have this startup script enabled : ( Preferences > Console > Advanced Options )

/usr/lib/python2.7/dist-packages/spyderlib/scientific_startup.py

If the standard PYTHONSTARTUP is enabled you won't have an interactive plot

Covich
  • 2,016
  • 1
  • 18
  • 31
0

Similar to @Rikki, I solved this problem by upgrading matplotlib with pip install matplotlib --upgrade. If you can't upgrade uninstalling and reinstalling may work.

pip uninstall matplotlib
pip install matplotlib
Seanny123
  • 6,594
  • 11
  • 56
  • 106
0

For me the problem happens if I simply create an empty matplotlibrc file under ~/.matplotlib on macOS. Adding "backend: macosx" in it fixes the problem.

I think it is a bug: if backend is not specified in my matplotlibrc it should take the default value.

an0
  • 16,371
  • 11
  • 78
  • 132
0

After running your code include:

import pylab as p
p.show()
Qiu
  • 5,222
  • 10
  • 45
  • 53
Shrish Trivedi
  • 189
  • 1
  • 5
0

I found that I needed window = Tk() and then window.mainloop()

Ralf Stubner
  • 24,387
  • 3
  • 31
  • 63
R Keene
  • 63
  • 2
-2

For Ubuntu 12.04:

sudo apt-get install python-qt4
virtualenv .env --no-site-packages
source .env/bin/activate
easy_install -U distribute
ln -s /usr/lib/python2.7/dist-packages/PyQt4 .
ln -s /usr/lib/python2.7/dist-packages/sip.so .
pip install matplotlib
nanvel
  • 1,304
  • 1
  • 13
  • 15