安装后无法运行matplotlib库的功能

时间:2019-07-18 06:43:52

标签: python matplotlib nltk

最近,在安装matplotlib之后,我无法运行其库函数。它显示错误消息,好像未安装matplotlib一样。

我已经在Windows 10计算机中安装了Python 3.7,并且对于解释器和IDLE都可以正常工作。安装matplotlib后,状态为ok:

"Successfully installed cycler-0.10.0 kiwisolver-1.1.0 matplotlib-3.1.1 numpy-1.16.4 pyparsing-2.4.0 python-dateutil-2.8.0"

但是当我尝试从matplotlib库运行任何函数时,它显示了一个错误:

代码:

from nltk.corpus import udhr

languages = ['Chickasaw', 'English', 'German_Deutsch', 'Greenlandic_Inuktikut', 'Hungarian_Magyar', 'Ibibio_Efik']
cfd = nltk.ConditionalFreqDist((lang, len(word)) for lang in languages for word in udhr.words(lang + '-Latin1'))
cfd.plot()

错误:

Traceback (most recent call last):

  File "C:\Users\hp\AppData\Local\Programs\Python\Python37-32\lib\site-packages\nltk\probability.py", line 1907, in plot
    from matplotlib import plt
ImportError: cannot import name 'plt' from 'matplotlib' (C:\Users\hp\AppData\Local\Programs\Python\Python37-32\lib\site-packages\matplotlib\__init__.py)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "<pyshell#6>", line 1, in <module>
    cfd.plot()
  File "C:\Users\hp\AppData\Local\Programs\Python\Python37-32\lib\site-packages\nltk\probability.py", line 1910, in plot
    'The plot function requires matplotlib to be installed.'
ValueError: The plot function requires matplotlib to be installed.See http://matplotlib.org/`

2 个答案:

答案 0 :(得分:0)

此行显示错误

from matplotlib import plt

将其更改为

from matplotlib import pyplot as plt
#or
import matplotlib.pyplot as plt

答案 1 :(得分:-1)

您使用的plt关键字是matplotlib.pyplot的缩写,因此首先您应该将其完全导入并为其分配一个名为plt的对象,如:

import matplotlib.pyplot as plt

然后为了绘制名为cfd的数据,您应该执行以下操作:

plt.plot(cfd_xdata,cfd_ydata)