24

I started to learn MatPlotLib using this tutorial for beginners. Here is the first example.

from pylab import *
X = np.linspace(-np.pi, np.pi, 256,endpoint=True)
C,S = np.cos(X), np.sin(X)

If I write these 3 lines into my python file and execute it in the command line (by typing python file_name.py), nothing happens. No error message, no plot.

Does anybody know why I do not see the plot?

ADDED

Of course I need to use show. But even if I add the following 3 lines:

plot(X,C)
plot(X,S)
show()

it still does no generate anything.

ADDED

Here are the lines that I use now:

import pylab as p
C = [1,2,3,4]
S = [10, 20, 30, 10]
p.plot(C,S)
p.show()

I still have the same result (nothing).

Roman
  • 124,451
  • 167
  • 349
  • 456

3 Answers3

54

It could be a problem with the backend. What is the output of python -c 'import matplotlib; import matplotlib.pyplot; print(matplotlib.backends.backend)'?

If it is the 'agg' backend, what you see is the expected behaviour as it is a non-interactive backend that does not show anything to the screen, but work with plt.savefig(...). You should switch to, e.g., TkAgg or Qt4Agg to be able to use show. You can do it in the matplotlib.rc file.

@shashank: I run matplotlib both on 12.04 and 12.10 without problems. In both cases I use the Qt4Agg backend. If you don't have the matplotlibrc set, the default backend is used. I'm sure that for Precise matplotlib repo was built with TkAgg. If the Quantal version has been built with e.g. Agg, then that would explain the difference

auselen
  • 27,577
  • 7
  • 73
  • 114
Francesco Montesano
  • 8,485
  • 2
  • 40
  • 64
  • 2
    thank you for the answer. Now I use `plt.savefig('name.png')` and `os.system('eog name.png &')` and get what I need. To be honest I do not know what "the 'agg' backend" means, but now I have everything what I need (the working solution). Thanks. – Roman Jan 28 '13 at 10:32
  • [here](http://mural.uv.es/parmur/matplotlib.pdf) you can find an explanation of what a backend is (sec 1.3). Your solution looks overly complicated when you have an in build mechanism to show the figure. Give a look to [this page](http://matplotlib.org/users/customizing.html) to learn about the matplotlibrc and how does it work. The best option would be to dump it into `~/.matplotlib/` and play with the backends untill you don't get `show` working (it's the first entry in the matplotlibrc file). – Francesco Montesano Jan 28 '13 at 11:03
  • 2
    You can find where the matplotlibrc file is by running: `import matplotlib matplotlib.matplotlib_fname()` See more here: http://matplotlib.org/users/customizing.html – stephenbez Oct 29 '15 at 07:12
  • Using 14.04 Ubuntu was able to fix the problem using Qt4Agg. – lv10 Mar 07 '16 at 16:49
  • 1
    And why not just use matplotlib.use('Agg') at the beginning? – amc Oct 30 '17 at 22:19
  • I fixed this by using `matplotlib.use('Qt4Agg')` before importing plt. This of course is because my mpl was compiled with qt4 support, however thing it defaulted to the agg backend when I launched python. – CMCDragonkai Oct 31 '17 at 11:15
  • It was TKAgg for me, still did not plot. What may be the reason? – AAI Aug 06 '19 at 23:35
  • I tried Qt4Agg and got `Failed to import any qt binding` – Bersan Apr 13 '21 at 14:17
  • You probably don't have Qt4 installed. Nowadays I would use `Qt5Agg`, though – Francesco Montesano Apr 13 '21 at 16:03
14

You need to call the function:

show()

to be more exact:

pylab.show()

and even better don't use:

from pylab import *

rather do:

import pylab as p:

and then:

X = np.linspace(-np.pi, np.pi, 256,endpoint=True)
C,S = np.cos(X), np.sin(X)

p.plot(C,S)
p.show()
oz123
  • 27,559
  • 27
  • 125
  • 187
  • sorry my question was incomplete (I have edited it). Even if I use `show`, it does not produce anything. – Roman Jan 28 '13 at 09:27
6

Try adding. I use Jupyter and this worked for me.

  %matplotlib inline