2017-05-26 157 views
1

我有一個數據幀的每一行數據:多條曲線使用Matplotlib

PNO,SID,SIZE,N3IC,S4IC,N4TC,KPAC,NAAC,ECTC 
SJV026,VIDAIC,FINE,0.0926,0.0446,0.0333,0.0185,0.005,0.0516 
SJV028,CHCRUC,FINE,0,0.1472,0.0076,0.0001,0.0025,0.0301 
SJV051,AMSUL,FINE,0,0.727,0.273,0,0,0 
SJV035,MOVES1,FINE,0.02,0.04092,0,0,0,0.45404 

我期待繪製該使用matplotlib或seaborn哪裏會有「N」的次要情節的數量爲每行的數據(即每行數據的一個條形圖)。

import matplotlib.pyplot as plt 
import pandas as pd 
from tkinter.filedialog import askopenfilename 
Inp_Filename = askopenfilename() 
df = pd.read_csv(Inp_Filename) 
rows, columns = df.shape 
fig, axes = plt.subplots(rows, 1, figsize=(15, 20)) 

count = 0 
for each in df.iterrows(): 
    row = df.iloc[count,3:] 
    row.plot(kind='bar') 

    count = count + 1 

plt.show() 

上面的代碼輸出不是我正在尋找的。有沒有辦法在上面的'無花果'和'軸'中繪製每行數據?

回答

1

原則上這種方法是正確的。在你的代碼中只有幾個錯誤,這些錯誤在糾正時會給出所需的結果。

import io 
import matplotlib.pyplot as plt 
import pandas as pd 

u = u"""PNO,SID,SIZE,N3IC,S4IC,N4TC,KPAC,NAAC,ECTC 
SJV026,VIDAIC,FINE,0.0926,0.0446,0.0333,0.0185,0.005,0.0516 
SJV028,CHCRUC,FINE,0,0.1472,0.0076,0.0001,0.0025,0.0301 
SJV051,AMSUL,FINE,0,0.727,0.273,0,0,0 
SJV035,MOVES1,FINE,0.02,0.04092,0,0,0,0.45404""" 

df = pd.read_csv(io.StringIO(u)) 
rows, columns = df.shape 
fig, axes = plt.subplots(rows, 1, sharex=True, sharey=True) 


for i, r in df.iterrows(): 
    row = df.iloc[i,3:] 
    row.plot(kind='bar', ax=axes[i]) 

plt.show() 

enter image description here

+0

感謝您對E(一)rnest! :) –