Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why matplotlib does not plot?

Tags:

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

like image 864
Roman Avatar asked Jan 28 '13 09:01

Roman


People also ask

Why is matplotlib not showing plot?

The issue actually stems from the matplotlib backend not being properly set, or from a missing dependency when compiling and installing matplotlib.

Why is matplotlib not working?

Occasionally, problems with Matplotlib can be solved with a clean installation of the package. In order to fully remove an installed Matplotlib: Delete the caches from your Matplotlib configuration directory. Delete any Matplotlib directories or eggs from your installation directory.

Why is my graph blank matplotlib?

The reason your plot is blank is that matplotlib didn't auto-adjust the axis according to the range of your patches. Usually, it will do the auto-adjust jobs with some main plot functions, such as plt. plot(), plt.


1 Answers

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

like image 55
Francesco Montesano Avatar answered Sep 23 '22 02:09

Francesco Montesano