My plot in ipython does not show with pyplot.show()
Asked Answered
S

17

200

Help required displaying matplotlib plots in ipython. I did not forget to call pyplot.show().

$ ipython --pylab

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

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

plt.show()

Nothing happens. No error message. No new window. I installed matplotlib with pip, and no error messages occurred.

Details:

I use,

  • Ubuntu
  • IPython v0.11
  • Python v2.6.6
  • matplotlib v1.0.1
Slav answered 23/9, 2011 at 20:27 Comment(1)
pylab is dead. Which is the recommended way to plot: matplotlib or pylab?. Disregard answers with pylab.Davedaveda
M
191

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:

Mesencephalon answered 23/9, 2011 at 20:49 Comment(0)
A
75

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...

Amimia answered 21/12, 2012 at 0:5 Comment(0)
C
46

Add %matplotlib inline once to the notebook, prior to plotting.

Cassowary answered 18/3, 2015 at 12:16 Comment(1)
This question is about ipython, not jupyter notebooksRotl
L
13

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.

Lohr answered 3/11, 2016 at 20:26 Comment(0)
H
11

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

Hoodoo answered 13/11, 2012 at 12:25 Comment(0)
G
10

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

Gasconade answered 16/9, 2016 at 5:30 Comment(1)
pylab is dead. Which is the recommended way to plot: matplotlib or pylab?Davedaveda
B
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
Butyrin answered 6/8, 2016 at 6:21 Comment(0)
C
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.

Commorant answered 2/6, 2016 at 15:9 Comment(0)
G
2

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
Geer answered 8/5, 2017 at 5:16 Comment(0)
V
1

After running your code include:

import pylab as p
p.show()
Variorum answered 25/8, 2017 at 11:30 Comment(1)
pylab is dead. Which is the recommended way to plot: matplotlib or pylab?Davedaveda
F
1

If you are working with yolov5 the fixes described here might not work as in my case. YOLOv5 developers have turned off the preview of the images using plt.show(), so most likely this will happen to you. To resolve make sure that your environment is correctly configured using requirements.txt file that comes with yolov5 and then use the workaround:

import torch
import matplotlib

model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # Load model first
matplotlib.use('TkAgg') # Change backend after loading model

as described here: https://github.com/ultralytics/yolov5/issues/2779 you can also try another backend from the ones mentioned above like 'Qt4Agg' or smth else.

Foothill answered 6/1, 2023 at 22:28 Comment(1)
This comment was absolutely brilliant - and saved the day. I had changed the backend at the start of the program (as I'd been doing some plot tests as it wasn't showing up), but never thought that Yolov5 - which I use - was re-setting it later on. This literally solved about 3-4 hours of hair-pulling (if only I had seen this earlier, I would have some hair left!)Awful
C
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

Crosspatch answered 13/5, 2014 at 13:34 Comment(0)
R
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.

Rollicking answered 13/7, 2017 at 20:30 Comment(0)
N
0

if you are a nube from ruby, don't forget the parenthesis - show()

Nichollenicholls answered 3/9, 2021 at 16:32 Comment(0)
M
0

This is not a great long-term solution, but if none of this is working for you, you can get around it by using this instead of p.show():

p.savefig("output_plot.png")

This will create the plot image in the same directory. You can open it separately in a normal image viewer.

Minx answered 8/4, 2023 at 1:28 Comment(0)
G
-1

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

Guideboard answered 15/7, 2018 at 20:6 Comment(0)
B
-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
Blunger answered 2/10, 2013 at 15:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.