1

I have data

city    inc pop edu crime   cult
New-York    29343,00    8683,00 0,00    10,40   0,00
Moscow  25896,00    17496,00    0,00    10,20   1,0
Rome    21785,00    15063,00    0,00    14,20   1,00
London  20000,00    70453,00    1,00    18,00   1,00
Berlin  44057,00    57398,00    1,00    6,30    1,00

I try to build plot and give name to plot and change color to columns

desire_salary = (df[(df['inc'] >= int(salary_people))])
fig = plt.figure()
result = desire_salary.pivot_table('city', 'cult', aggfunc='count').plot(kind='bar', alpha=0.75, rot=0, label="Presence / Absence of cultural centre")
plt.xlabel("Cultural centre")
plt.ylabel("Frequency")
plt.set_title('Salary and culture')
plt.plot(result[[0]], color='red')
plt.plot(result[[1]], color='blue')
plt.show()

But it returns error AttributeError: 'module' object has no attribute 'set_title' and TypeError: 'AxesSubplot' object has no attribute '__getitem__'

1
  • Write plt.title("Some title") Commented Jun 9, 2016 at 11:24

2 Answers 2

2

You are trying to use the set_title method from the AxesSubplot class, but you are not using the mpl object-oriented approach. There are two ways to correct this:

  1. Use plt.title('Salary and culture')

  2. Switch to the more flexible OO approach, and set the title and axes labels using the relevant Axes methods, such as ax.set_title, ax.set_xlabel, etc.

The source of your second error is that when you call .plot on the pivot_table, you return a matplotlib AxesSubplot object, not the DataFrame. So then you try to plot result[[0]], it is trying to index an AxesSubplot object.

desire_salary = (df[(df['inc'] >= int(salary_people))])
fig = plt.figure()

# Create the pivot_table
result = desire_salary.pivot_table('city', 'cult', aggfunc='count')

# plot it in a separate step. this returns the matplotlib axes
ax = result.plot(kind='bar', alpha=0.75, rot=0, label="Presence / Absence of cultural centre", ax=ax)

ax.set_xlabel("Cultural centre")
ax.set_ylabel("Frequency")
ax.set_title('Salary and culture')

ax.plot(result[[0]], color='red')
ax.plot(result[[1]], color='blue')
plt.show()
Sign up to request clarification or add additional context in comments.

3 Comments

I have no first error, but second TypeError: 'AxesSubplot' object has no attribute '__getitem__' didn't dissapear
Ah, I see your problem. When you call plot on the pivot_table, that returns the axes it is plotted on, not the pivot table. I'll edit the answer
It's strange. Color didn't change. It's blue to all columns
0

to try "return_type = 'dict'" for TypeError: 'AxesSubplot' object has no attribute 'getitem'

1 Comment

When giving an answer it is preferable to give some explanation as to WHY your answer is the one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.