我能导入 matplotlib,但是当我尝试运行以下代码时:
matplotlib.pyplot(x)
我得到:
Traceback (most recent call last):
File "<pyshell#31>", line 1, in <module>
matplotlib.pyplot(x)
AttributeError: 'module' object has no attribute 'pyplot'
我能导入 matplotlib,但是当我尝试运行以下代码时:
matplotlib.pyplot(x)
我得到:
Traceback (most recent call last):
File "<pyshell#31>", line 1, in <module>
matplotlib.pyplot(x)
AttributeError: 'module' object has no attribute 'pyplot'
pyplot
是matplotlib
的一个子模块,但它不能仅通过import matplotlib
导入:
>>> 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
>>>
plt
:import matplotlib.pyplot as plt
p = plt.plot(...)
你导入了吗?仅导入matplotlib
是不够的。
>>> 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
>>> matplotlib.pyplot
工作。
pyplot是matplotlib的一个子模块,在导入matplotlib时不会立即导入。
导入pyplot的最常见形式是
import matplotlib.pyplot as plt
因此,您的陈述不会太长,例如:
plt.plot([1,2,3,4,5])
取代matplotlib.pyplot.plot([1,2,3,4,5])
而且:pyplot
不是函数,而是一个模块!所以不要调用它,而是使用在这个模块内部定义的函数。请参考上面的示例。