我有一个动态的 Pandas 数据框。我正在尝试将条形图放置在一个子图中,该子图中将在单个窗口中显示多个图形。在 x 轴上,有“面积”,在 y 轴上,有面积计数。
Names Area Area Count
0 Info 1 [LOCATION, PERSON] [130, 346]
1 Info 2 [NRP] [20]
rows = len(df1['Names'])
fig, ax = plt.subplots(nrows=rows, ncols=1)
df1['Area'].plot(ax=ax[0,0])
df1['Area Count'].plot(ax=ax[0,1])
pandas.Series.explode
。import pandas as pd
import seaborn as sns
# test dataframe
data = {'Names': ['Info 1', 'Info 2'], 'Area': [['LOCATION', 'PERSON'], ['NRP']], 'Area Count': [[130, 346], [20]]}
df = pd.DataFrame(data)
# display(df)
Names Area Area Count
0 Info 1 [LOCATION, PERSON] [130, 346]
1 Info 2 [NRP] [20]
# explode the lists
df = df.set_index('Names').apply(pd.Series.explode).reset_index()
# display(df)
Names Area Area Count
0 Info 1 LOCATION 130
1 Info 1 PERSON 346
2 Info 2 NRP 20
df.plot.bar(x='Area', y='Area Count')
sns.barplot(data=df, x='Area', y='Area Count', hue='Names', dodge=False)
df.pivot(index='Area', columns='Names', values='Area Count').plot.bar()
df.pivot(index='Names', columns='Area', values='Area Count').plot.bar()
sns.catplot(data=df, col='Names', x='Area', y='Area Count', kind='bar', estimator=sum)
rows = len(df.Names.unique())
fig, ax = plt.subplots(nrows=rows, ncols=1, figsize=(6, 8))
for i, v in enumerate(df.Names.unique()):
data = df[df.Names == v]
data.plot.bar(x='Area', y='Area Count', title=v, ax=ax[i], legend=False)
plt.tight_layout()
plt.show()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句