Arseniy Krupenin
Arseniy Krupenin

Reputation: 3880

Error matplotlib

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__'

Upvotes: 1

Views: 2047

Answers (2)

vanjune
vanjune

Reputation: 1

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

Upvotes: 0

tmdavison
tmdavison

Reputation: 69056

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()

Upvotes: 2

Related Questions