Error when “import matplotlib.pyplot as plt”

爱⌒轻易说出口 提交于 2019-12-23 10:50:53

问题


I am very new to python. So, my problem might be too simple to be solved. But I cannot. Please help me! I did not have any problem to use "plt", but it suddenly shows error message and does not work, when I import it. Please see the below.

    >>> import matplotlib
    >>> import matplotlib.pyplot as plt
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/lib64/python2.6/site-packages/matplotlib/pyplot.py", line 6, in <module>
        from matplotlib.figure import Figure, figaspect
     File "/usr/lib64/python2.6/site-packages/matplotlib/figure.py", line 18, in <module>
       from axes import Axes, SubplotBase, subplot_class_factory
     File "/usr/lib64/python2.6/site-packages/matplotlib/axes.py", line 2, in <module>
       import math, sys, warnings, datetime, new
     File "new.py", line 12, in <module>
       import matplotlib.pyplot as plt
     AttributeError: 'module' object has no attribute 'pyplot'

This package is suddenly corrupted? So, I tried to install as below.I use ubuntu.

    In [1]: sudo apt-get install python-matplotlib
      File "<ipython-input-1-2400ac877ebd>", line 1
        sudo apt-get install python-matplotlib
       ^
    SyntaxError: invalid syntax

If I need to reinstall, please give me very detailed instruction how to do it. Thank you very much in advance.


回答1:


Try to type sudo apt-get install python-matplotlib in a terminal. In your message you typed it in ipython console. This is a bash command, not a python one.

You need to quit ipython before. For that use Ctrl + D




回答2:


I have this problem before.

What the conditions of mine is that I have install multiply versions of python and numpy in my OS. That's why the python can't find the right numpy to support the matplotlib.

The method I choose to solved this problem is the modify the $PYTHONPATH by editing the file .bashrc in my home path.

Change the order of the python install path shown, like putting the path "/usr/lib/python2.7/dist-packages" in the front of the $PYTHONPATH.

Maybe you can find something amazing. Good luck for you!!




回答3:


something wrong with tkinter I guess, try adding:

import matplotlib as mpl
mpl.use('TkAgg')

in the front of import matplotlib.pyplot as plt.



来源:https://stackoverflow.com/questions/24302091/error-when-import-matplotlib-pyplot-as-plt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!