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

5
  • 2
    don't you have to show it at the end using show() ? Commented Jan 28, 2013 at 9:23
  • @Roman are you using Ubuntu 12.10? For me everything was working on ubuntu 12.04 but as soon as I moved to 12.10 everything, plots stopped showing up. I am assuming its a distro related issue in my case at-least. savefig(filename) still works though. Commented Jan 28, 2013 at 9:29
  • @Shashank Singh, no, I use 12.04. Commented Jan 28, 2013 at 9:32
  • import matplotlib.pyplot as plt and then plt.show() ? Commented Jan 28, 2013 at 9:46
  • @Dan, it does not help. I still have no result. Commented Jan 28, 2013 at 10:18

3 Answers 3

55

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

Sign up to request clarification or add additional context in comments.

9 Comments

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.
here 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 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).
You can find where the matplotlibrc file is by running: import matplotlib matplotlib.matplotlib_fname() See more here: matplotlib.org/users/customizing.html
Using 14.04 Ubuntu was able to fix the problem using Qt4Agg.
And why not just use matplotlib.use('Agg') at the beginning?
|
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()

1 Comment

sorry my question was incomplete (I have edited it). Even if I use show, it does not produce anything.
7

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

  %matplotlib inline

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.