Python matplotlib.show() 错误:模块“matplotlib”没有属性“show”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45150238/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:46:21  来源:igfitidea点击:

Error with matplotlib.show() : module 'matplotlib' has no attribute 'show'

pythonmatplotlib

提问by William

I'm a Python 3.6 user and I've been trying to learn how to use the matplotlib and pandas libraries. But as I try to use the "show()" function, I get the following error:

我是 Python 3.6 用户,我一直在尝试学习如何使用 matplotlib 和 pandas 库。但是当我尝试使用“show()”函数时,出现以下错误:

import pandas as pd
import matplotlib as plt
df=pd.DataFrame({'Day':[1,2,3], 'Revenue':[100,200,320]})
df.plot()
plt.show()

ERROR: AttributeError: module 'matplotlib' has no attribute 'show'

错误:AttributeError:模块“matplotlib”没有属性“show”

回答by Rory Daulton

Do not use

不使用

import matplotlib as plt

but rather use

而是使用

import matplotlib.pyplot as plt

pltis an abbreviation for pyplot, which is a module inside the matplotlibpackage. You need to address it for the kinds of things you are doing, not just matplotlib.

plt是 的缩写pyplot,是matplotlib包内的一个模块。你需要针对你正在做的事情来解决它,而不仅仅是matplotlib.

Note that matplotlibcan be used without using pyplotat all, but most people find it easier to use pyplot. See its documentationor the tutorialfor details.

请注意,matplotlib可以在不使用的情况下使用pyplot,但大多数人发现使用pyplot. 有关详细信息,请参阅其文档教程