属性错误:模块“matplotlib”没有“xlabel”属性。

3

我的代码是:

import matplotlib as plt 
sns.distplot(CMSU['Spending'], kde = False)
plt.xlabel("Spending", size=15)
plt.ylabel("Probablity", size=15)
plt.title("Distribution for the variable - Spending", size=18);

我遇到了以下错误:

AttributeError                            Traceback (most recent call last)
<ipython-input-32-1c6eb744a592> in <module>
      1 sns.distplot(CMSU['Spending'], kde = False)
----> 2 plt.xlabel("Spending", size=15)
      3 plt.ylabel("Probablity", size=15)
      4 plt.title("Distribution for the variable - Spending", size=18);

AttributeError: module 'matplotlib' has no attribute 'xlabel'

可能会出现什么问题呢?


你能分享一下 plt 是如何被创建/初始化的吗? - Mureinik
3
问题在于你忘记在发布的代码中添加 import。你需要添加 from matplotlib import pyplot as plt 而不是类似于 import matplotlib as plt 的语句。 - JohanC
@Mureinik 是的。这是代码:import matplotlib as plt - Simran Munot
@JohanC 收到,谢谢! - Simran Munot
4个回答

7
你的 import 语句有误。那些方法属于 pyplot。也就是说,你应该像这样导入它:
import matplotlib.pyplot as plt

1

我认为你需要做import matplotlib.pyplot as plt而不是仅仅使用matplotlib as plt,因为xlabel和可能其他各种函数在matplotlib中不存在。


1

使用:

matplotlib.pyplot.xlabel()

同样适用于 ylabel title

matplotlib.pyplot.ylabel()
matplotlib.pyplot.title()

from matplotlib import pyplot as plt 是标准的,而且更容易。 - MattDMo
@MattDMo 是的,还有 import matplotlib.pyplot as plt 也被使用了。 - monsieuralfonse64

0

属性错误 追溯(最近一次调用最新的) 在<cell line: 4>() 2 plt.title("Videos are strongly correlated with clicking") 3 plt.bar(["Page Load", "Video Watching", "Reading Pricing"], [-0.39, 0.67, 0.0004]) ----> 4 plt.ylabl("Correlation With Clicking")

属性错误:模块“matplotlib.pyplot”没有属性“ylabl”。 我之前运行过这段代码,而且成功了,现在不知道怎么回事!!!


目前你的回答不够清晰,请编辑并添加更多细节,以帮助其他人理解它如何回答问题。你可以在帮助中心找到有关如何编写好答案的更多信息。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接