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

ujv3wf0j  于 2023-05-16  发布在  Python
关注(0)|答案(4)|浏览(319)

我的代码是:

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'

有什么可能出错呢?

zyfwsgd6

zyfwsgd61#

你的import语句是错误的。这些方法属于pyplot。也就是说,你应该像这样导入它:

import matplotlib.pyplot as plt
ylamdve6

ylamdve62#

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

nukf8bse

nukf8bse3#

用途:

matplotlib.pyplot.xlabel()

ylabeltitle也是一样:

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

v6ylcynt4#

AttributeError Traceback(most recent call last)in()2 plt. title("Videos are strongly correlated with clicking")3 www.example.com(["Page Load","Video Watching","Reading Pricing"],[-0.39,0.67,0.0004])----> 4 plt. ylabl("Correlation With Clicking")<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")
属性错误:module 'matplotlib. pyplot' has no attribute 'ylabl我以前运行过同样的代码,结果成功了,不知道现在发生了什么!!!

相关问题