66

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'

3 Answers 3

64

pyplot is a submodule of matplotlib, but it doesn't get imported with import matplotlib only:

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

However, it is recommended to alias it as plt:

import matplotlib.pyplot as plt

Then, you can use the various functions and classes it contains:

p = plt.plot(...)
Sign up to request clarification or add additional context in comments.

4 Comments

Tried this originally, get an ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/_path.so, 2): no suitable image found. Did find: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/m
This looks like your installation is corrupt, maybe try a clean reinstall.
Worked it out.. apparently the current matplotlib version from the installer is not compatible with the 64-bit version of Python so downloaded 32-bit version and works perfectly now. Thanks for the help though.
I'm happy to improve on this answer :-). Leave a comment about what's wrong and I'll do my best to address it!
44

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

3 Comments

Had tried this and it wasn't the problem. Worked it out.. apparently the current matplotlib version from the installer is not compatible with the 64-bit version of Python so downloaded 32-bit version and works perfectly now. Thanks for the help though.
Sure, always install the the version of a library corresponding to the version of you interpreter...
I have the same problem with pyplot. But "import matplotlib.pyplot as plt" still doesn't work for me. I get the same error.
0

simple answer worked for me if you are importing 'librosa' library, just import it before 'import matplotlib.pyplot as plt'. That worked for me and couple others for similar issue

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.