111

I just installed matplotlib in Ubuntu 9.10 using the synaptic package system. However, when I try the following simple example

>>> from pylab import plot;
>>> plot([1,2,3],[1,2,3])
[<matplotlib.lines.Line2D object at 0x9aa78ec>]

I get no plot window. Any ideas on how to get the plot window to show?

2
  • 9
    pylab doesn't show the plot by default, since drawing and constant updating can be expensive. You need to do an explicit show(). Commented Jan 25, 2010 at 8:15
  • 1
    Some official documentation on this issue: matplotlib.org/faq/…
    – stephenbez
    Commented Apr 21, 2014 at 18:06

13 Answers 13

169

You can type

import pylab
pylab.show()

or better, use ipython -pylab.


Since the use of pylab is not recommended anymore, the solution would nowadays be

import matplotlib.pyplot as plt

plt.plot([1,2,3])

plt.show()
39

pylab.show() works but blocks (you need to close the window).

A much more convenient solution is to do pylab.ion() (interactive mode on) when you start: all (the pylab equivalents of) pyplot.* commands display their plot immediately. More information on the interactive mode can be found on the official web site.

I also second using the even more convenient ipython -pylab (--pylab, in newer versions), which allows you to skip the from … import … part (%pylab works, too, in newer IPython versions).

5
  • 1
    but you need to call pylab.show() first to even open a figure.
    – Steve Tjoa
    Commented Jan 25, 2010 at 14:46
  • 3
    @Steve: my experience is that this strongly depends on the backend. On Mac OS X with the regular Python shell, I don't have to do show() (or draw(), for that matter): plot() opens a new window. Commented Jan 25, 2010 at 16:46
  • 1
    Ah, okay. Good point. I only have experience with ipython in Linux.
    – Steve Tjoa
    Commented Jan 25, 2010 at 18:42
  • draw() doesn't work for me, but pylab.ion() works. I'm using OSx Commented Dec 5, 2013 at 5:51
  • Same thing here in the Python shell with the latest Matplotlib. I updated my answer, thanks. Commented Dec 6, 2013 at 10:20
20

Try this:

import matplotlib
matplotlib.use('TkAgg') 

BEFORE import pylab

0
11

The code snippet below works on both Eclipse and the Python shell:

import numpy as np
import matplotlib.pyplot as plt

# Come up with x and y
x = np.arange(0, 5, 0.1)
y = np.sin(x)

# Just print x and y for fun
print x
print y

# Plot the x and y and you are supposed to see a sine curve
plt.plot(x, y)

# Without the line below, the figure won't show
plt.show()
1
  • As many other answers clarify, this may work for you at the moment, but it all depends on how your matplotlib backend is configured.
    – nealmcb
    Commented Nov 23, 2014 at 21:53
10

Any errors show up? This might an issue of not having set the backend. You can set it from the Python interpreter or from a config file (.matplotlib/matplotlibrc) in you home directory.

To set the backend in code you can do

import matplotlib
matplotlib.use('Agg')

where 'Agg' is the name of the backend. Which backends are present depend on your installation and OS.

http://matplotlib.sourceforge.net/faq/installing_faq.html#backends

http://matplotlib.org/users/customizing.html

1
  • 1
    Totally works, thanks. To be clear, the simplest thing to do (once you konw this is working of course) is to set your backend in .matplotlib/matplotlibrc to backend: Agg. Then you don't have to explicitly use one every time :)
    – dsummersl
    Commented Aug 25, 2014 at 0:37
2

Modern IPython uses the "--matplotlib" argument with an optional backend parameter. It defaults to "auto", which is usually good enough on Mac and Windows. I haven't tested it on Ubuntu or any other Linux distribution, but I would expect it to work.

ipython --matplotlib
0

If you encounter an issue in which pylab.show() freezes the IPython window (this may be Mac OS X specific; not sure), you can cmd-c in the IPython window, switch to the plot window, and it will break out.

Apparently, future calls to pylab.show() will not freeze the IPython window, only the first call. Unfortunately, I've found that the behavior of the plot window / interactions with show() changes every time I reinstall matplotlib, so this solution may not always hold.

0

If you are starting IPython with the --pylab option, you shouldn't need to call show() or draw(). Try this:

ipython  --pylab=inline
0

--pylab no longer works for Jupyter, but fortunately we can add a tweak in the ipython_config.py file to get both pylab as well as autoreload functionalities.

c.InteractiveShellApp.extensions = ['autoreload', 'pylab']
c.InteractiveShellApp.exec_lines = ['%autoreload 2', '%pylab']
0

If you are user of Anaconda and Spyder then best solution for you is that :

Tools --> Preferences --> Ipython console --> Graphic Section

Then in the Support for graphics (Matplotlib) section:

select two avaliable options

and in the Graphics Backend:

select Automatic

0
import numpy as np
import matplotlib.pyplot as plt
import pylab

plt.plot(y)

pylab.show(block=False)

plt.pause(3) #After 3 seconds the plot closes.
1
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, can you edit your answer to include an explanation of what you're doing and why you believe it is the best approach? Commented Apr 20, 2023 at 5:14
0

%matplotlib inline

use this line whenever your plot with any library hides in the backend of jupyter notebook.

This works fine for me.

-5

Another possibility when using easy_install is that you need to require the most recent version of matplotlib. Try:

import pkg_resources
pkg_resources.require("matplotlib")

before you import matplotlib or any of its modules.

1
  • 2
    This just makes sure matplotlib is installed, not affect how the window handling is done.
    – tacaswell
    Commented Aug 9, 2013 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.