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

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

我的代码是:

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'

可能会出什么问题?

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

使用:

matplotlib.pyplot.xlabel()

ylabeltitle 相同:

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

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

import matplotlib.pyplot as plt