2016-06-29 70 views
1

我有一個大熊貓數據幀是這樣的:雙圖形與matplotlib:屬性錯誤

nan 0  ingredient contribution count 
0 0.0 cracker  0.088844873  11 
1 2.0 water   0.044386494  125 
2 3.0 oil   0.034567456  10 
3 4.0 flour   0.030855063  186 
... 

我想創建一個雙圖,看起來是這樣的: figure

我試過的代碼:

import matplotlib.pyplot as plt #importing libraries 
import numpy as np 

plt.figure(1) #creating empty figure 

t = np.arange(0.0, 2.0, 0.01) 

fig_contr = df[['ingredient','contribution']] #selecting columns 
fig_count = df[['ingredient','count']] 

plt.subplot(211) #creating subplot 
plt.plot(t, fig_contr) #plotting subplot 
plt.subplot(212) 
plt.plot(t, fig_count) 

但我得到這個錯誤:AttributeError的: '據幀' 對象有沒有屬性「找到

我應該如何創建我想獲得的圖形?

回答

1

一個可能的解決方案是使用Series.plot.bar

plt.figure(1) #creating empty figure 

df.set_index('ingredient', inplace=True) #set index from column ingredient 

fig_contr = df['contribution'] #selecting columns 
fig_count = df['count'] 

plt.subplot(211) #creating subplot 
fig_contr.plot.bar() #plotting subplot 
plt.subplot(212) 
fig_count.plot.bar() 
plt.show() 

graph

您還可以更改軸標籤的方向X:

plt.figure(1) #creating empty figure 
df.set_index('ingredient', inplace=True) 

fig_contr = df['contribution'] #selecting columns 
fig_count = df['count'] 

plt.subplot(211) #creating subplot 
fig_contr.plot.bar(rot=0) #plotting subplot 
plt.subplot(212) 
fig_count.plot.bar(rot=0) 
plt.show() 

graph1

+0

感謝jezrael!不幸的是我得到的錯誤消息:TypeError:空'DataFrame':沒有數字數據繪圖。你有一個想法如何擺脫這一點? – Papie

+0

什麼是'print df.dtypes'? – jezrael

+0

dtype是一個對象。你的情況是整數/浮點數嗎? – Papie