Python matplotlib 没有属性“pyplot”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14812342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
matplotlib has no attribute 'pyplot'
提问by hanachronism
I can import matplotlib but when I try to run the following:
我可以导入 matplotlib 但是当我尝试运行以下命令时:
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'
回答by mgilson
pyplotis a sub-module of matplotlibwhich doesn't get imported with a simple import matplotlib.
pyplot是一个子模块,matplotlib它不会通过简单的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 pltat which time you can use the various functions and classes it contains:
似乎习惯这样做: import matplotlib.pyplot as plt此时您可以使用它包含的各种函数和类:
p = plt.plot(...)
回答by Thorsten Kranz
Did you import it? Importing matplotlibis not enough.
你导入了吗?进口matplotlib还不够。
>>> 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.
pyplot 是 matplotlib 的子模块,在导入 matplotlib 时不会立即导入。
The most common form of importing pyplot is
导入 pyplot 的最常见形式是
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: pyplotis not a function, it's a module! So don't call it, use the functions defined insidethis module instead. See my example above
并且:pyplot不是一个函数,它是一个模块!所以,不要把它,使用定义的函数里面这个模块来代替。看我上面的例子

