2016-06-09 55 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)。 –