我这里有一个相同的数据帧。
type c1 c2 c3 c4 c5 c6
A 0 20 14 4 100 0
B 10 30 23 9 12 0
C 20 10 0 20 24 34我想要绘图条形图相同的图像。matplotlib python

发布于 2020-08-12 17:45:18
我转换了数据的格式,使用'type‘作为索引,并以一行、三列的格式输出,并绘制了一个'pandas’图。
import matplotlib.pyplot as plt
import pandas as pd
import io
data = '''
type c1 c2 c3 c4 c5 c6
A 0 20 14 4 100 0
B 10 30 23 9 12 0
C 20 10 0 20 24 34
'''
df = pd.read_csv(io.StringIO(data), sep='\s+', index_col=0)
df = df.T
df.plot(kind='bar', subplots=True, layout=(1,3))
plt.show()

发布于 2020-08-12 17:51:01
只需要使用show()来保存每个图
import matplotlib.pyplot as plt
data = """type c1 c2 c3 c4 c5 c6
A 0 20 14 4 100 0
B 10 30 23 9 12 0
C 20 10 0 20 24 34"""
a = [[t for t in l.split(" ") if t!=""] for l in data.split("\n")]
df = pd.DataFrame(a[1:], columns=a[0])
df = df.astype({c:"int64" for c in df.columns if "c" in c})
for i, r in df.iterrows():
df.iloc[i, 1:].T.plot.bar(title=df.loc[i, "type"])
plt.show()https://stackoverflow.com/questions/63373604
复制相似问题