AttributeError:模块“matplotlib”没有属性“xlabel”

2024-09-29 17:18:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码是:

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'

有什么可能出错


Tags: falseforsizetitlematplotlibpltdistributionsns
3条回答

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

import matplotlib.pyplot as plt

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

使用:

matplotlib.pyplot.xlabel()

ylabeltitle相同:

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

相关问题 更多 >

    热门问题