AttributeError:“function”对象没有属性“xlabel” - 使用 matplotlib 绘图时没有显示轴标签或标题

问题描述 投票:0回答:1

我正在绘制一个条形图,显示最终收养动物的收容所年龄组,但我不断收到错误:“AttributeError:'function'对象没有属性'xlabel'”

所以我的图没有显示 y 轴标签或没有图标题。我已将 matplotlib 导入为 plt。

这是我的代码:

import pandas as pd    
import matplotlib as plt

# ensuring 'Age Range at Intake' column is string type:
shelter_df['Animal', 'Age Range at Intake'] = shelter_df['Age Range at Intake'].astype(str)

# Filtering data for adoption:
adoptionData = shelter_df[shelter_df['Outcome Type'] == 'Adoption']

# Grouping and counting data by Age Range at Intake and Animal type:
ageAnimalAdoptCounts = adoptionData.groupby(['Age Range at Intake', 'Animal']).size().unstack()

# plotting a bar chart to show Animal Types adopted per Age Group:
ax = ageAnimalAdoptCounts.plot(kind = 'bar', stacked = True, figsize = (12, 6))
plt.figure(figsize = (12, 6))
ax.set.xlabel('Age Range at Intake')
ax.set.ylabel('Count')
ax.set.title('Animal Types adopted per Age Group at Intake')
ax.set.xticklabels(ax.get.xticklabels(), rotation = 45, ha = 'right')

plt.tight_layout()
plt.show()

Chart showing no axis labels or title

有人知道可能出了什么问题吗?

我尝试将 matplotlib.pyplot 导入为 plt 但没有什么区别。我希望 y 轴标签和标题显示在绘图上。谢谢你。

matplotlib attributeerror
1个回答
0
投票

xlabel
是一个关键字参数
set
可以获取,而不是一个属性:

ax.set(xlabel = 'Age Range at Intake')

以下语句也应类似修改。

© www.soinside.com 2019 - 2024. All rights reserved.