Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib has no attribute 'pyplot'

I can import matplotlib but when I try to run the following:

matplotlib.pyplot(x) 

I get:

Traceback (most recent call last):    File "<pyshell#31>", line 1, in <module>        matplotlib.pyplot(x) AttributeError: 'module' object has no attribute 'pyplot' 
like image 658
hanachronism Avatar asked Feb 11 '13 12:02

hanachronism


People also ask

What is Matplotlib PyLab?

MatPlotLib with Python PyLab is a procedural interface to the Matplotlib object-oriented plotting library. Matplotlib is the whole package; matplotlib. pyplot is a module in Matplotlib; and PyLab is a module that gets installed alongside Matplotlib. PyLab is a convenience module that bulk imports matplotlib.

Why is %Matplotlib inline?

Why matplotlib inline is used. You can use the magic function %matplotlib inline to enable the inline plotting, where the plots/graphs will be displayed just below the cell where your plotting commands are written. It provides interactivity with the backend in the frontends like the jupyter notebook.

How do I display Axessubplot?

To show an axes subplot in Python, we can use show() method. When multiple figures are created, then those images are displayed using show() method.


2 Answers

pyplot is a sub-module of matplotlib which doesn't get imported with a simple import matplotlib.

>>> import matplotlib >>> print matplotlib.pyplot Traceback (most recent call last):   File "<stdin>", line 1, in <module> AttributeError: 'module' object has no attribute 'pyplot' >>> import matplotlib.pyplot >>>  

It seems customary to do: import matplotlib.pyplot as plt at which time you can use the various functions and classes it contains:

p = plt.plot(...) 
like image 73
mgilson Avatar answered Sep 19 '22 03:09

mgilson


Did you import it? Importing matplotlib is not enough.

>>> import matplotlib >>> matplotlib.pyplot Traceback (most recent call last):   File "<stdin>", line 1, in <module> AttributeError: 'module' object has no attribute 'pyplot' 

but

>>> import matplotlib.pyplot >>> matplotlib.pyplot 

works.

pyplot is a submodule of matplotlib and not immediately imported when you import matplotlib.

The most common form of importing pyplot is

import matplotlib.pyplot as plt 

Thus, your statements won't be too long, e.g.

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

instead of

matplotlib.pyplot.plot([1,2,3,4,5]) 

And: pyplot is not a function, it's a module! So don't call it, use the functions defined inside this module instead. See my example above

like image 45
Thorsten Kranz Avatar answered Sep 17 '22 03:09

Thorsten Kranz