2016-06-09 46 views
1

我有数据错误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 
Berlin 44057,00 57398,00 1,00 6,30 1,00 

我尝试建立的情节和plot给出名称和改变颜色,列

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' object has no attribute 'set_title'和​​

+0

写'plt.title( “有些题”)' – Serenity

回答

2

你是试图使用AxesSubplot类中的set_title方法,但是您没有使用mpl面向对象的方法。有两种方法可以解决此:

  1. 使用plt.title('Salary and culture')

  2. 切换到更灵活的面向对象的方法,并设置标题和轴标签使用相关Axes方法,如ax.set_titleax.set_xlabel等。

你的第二个错误的来源是,当你在pivot_table.plot,你返回matplotlib AxesSubplot对象,而不是DataFrame。那么你试图绘制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() 
+0

我没有第一个错误,但第二个'TypeError:'AxesSubplot'对象没有属性'__getitem __''没有消失 –

+0

啊,我看到你的问题。当您在pivot_table上调用'plot'时,将返回绘制的坐标轴,而不是数据透视表。我会编辑答案 – tom

+0

这很奇怪。颜色没有改变。它对所有列都是蓝色的 –

0

尝试 “return_type = '字典'” 为类型错误: 'AxesSubplot' 对象有没有属性 '的GetItem'

+1

当给出答案时,最好给出[关于为什么你的答案的解释](http://stackoverflow.com/help/how-to-answer)。 –