Python 错误matplotlib
我有数据Python 错误matplotlib,python,matplotlib,Python,Matplotlib,我有数据 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 B
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
我尝试构建plot并命名plot
,并将颜色更改为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()
但是它返回错误
AttributeError:“module”对象没有属性“set\u title”
和TypeError:“AxesSubplot”对象没有属性“\uu getitem\uuuu”
您试图使用AxesSubplot
类中的set\u title
方法,但您没有使用mpl面向对象的方法。有两种方法可以纠正此问题:
plt.title('Salary and culture')
axes
方法设置标题和轴标签,例如ax.set\u title
,ax.set\u xlabel
,等等pivot\u表
上调用.plot
时,返回的是matplotlibAxesSubplot
对象,而不是数据框
。因此,如果您尝试绘制result[[0]]
,它将尝试为AxesSubplot
对象编制索引
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()
要为TypeError尝试“return_type='dict'”:'AxesSubplot'对象没有属性'getitem“Write
plt.title(“某些标题”)
我没有第一个错误,但第二个TypeError:'AxesSubplot'对象没有属性'\uu getitem'
没有发现问题。在pivot_表上调用plot
时,将返回绘制轴,而不是pivot表。我会编辑答案,这很奇怪。颜色没有改变。所有的专栏都是蓝色的当给出答案时,最好是给出一个答案。