-2

I am plotting a bar graph showing Age Group at Intake of shelter Animals eventually adopted but I keep getting the error: "AttributeError: 'function' object has no attribute 'xlabel'"

So my plot is showing no y axis labels or no plot title. I have imported matplotlib as plt.

Here's my code:

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

Does anyone have any ideas what could be wrong?

I've tried importing matplotlib.pyplot as plt but doesn't make a difference. I want the y axis labels and title to be displayed on the plot. Thank you.

Querty
  • 1
  • 1

1 Answers1

0

xlabel is a keyword argument set can get, not a property:

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

The following statements should also be modified similarly.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Thanks, that way gave me a syntax error but worked when I put '=' i.e 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') However although the chart looks fine, I am getting a new error: AttributeError: 'Axes' object has no attribute 'get' So it seems to be a problem with 'ax.set(xticklabels = ax.get.xticklabels()'. Any thoughts please? – Querty Aug 12 '23 at 18:10
  • 1
    Actually it's fine now. I just deleted 'ax.set(xticklabels = ax.get.xticklabels()' line completely after: ax.set(xlabel = 'Age Range at Intake') ax.set(ylabel = 'Count') ax.set(title = 'Animal Types adopted per Age Group at Intake') – Querty Aug 12 '23 at 18:54