我有一个如下所示的数据框:
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()
以上代码输出不是我要找的。有没有办法在上面的'fig'和'axes'中绘制每一行数据?
答案 0 :(得分: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()