如何使用seaborn绘制多个酒吧的酒吧聊天

2024-03-29 07:02:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用数据绘制条形图,但我不知道如何用多个条形图绘制seaborn条形图(即,我想在“裁判员id”和3个条形图上绘制每个显示赢/平/输、y数量的数据框:

t

使用sns.barplot()打印时,它会堆叠/分层所有数量

我的代码:

figure = plt.figure(figsize=(14,6))
subplot = figure.add_subplot()
sns.barplot(all_referees['Field_referee_id'],all_referees['Quantity_lost'],  color="tomato", label = "lost")
sns.barplot(all_referees['Field_referee_id'],all_referees['Quantity_draw'],  color="yellow", label = "draw")
sns.barplot(all_referees['Field_referee_id'],all_referees['Quantity_won'],  color="limegreen", label = "won")
for item in subplot.get_xticklabels():
    item.set_rotation(45)
plt.xticks(ha='right',fontweight='light',fontsize='large')
plt.legend(loc='upper right', frameon=False)

而且:

  • 如何创造条件,只显示至少判了7场比赛(输球+平局+胜出>;=7场)的裁判
  • 如何在每个裁判员的三杆之间留出宽度间隙

Tags: idfield绘制pltalllabelquantitycolor
1条回答
网友
1楼 · 发布于 2024-03-29 07:02:34

首先需要将数据帧的形状从宽改为长

例如:

df = pd.DataFrame({'ID': list('abcd'), 'Quantity_Lost':[3,5,3,4], 'Quantity_Draw':[2,2,1,None], 'Quantity_Won': [5,1,4,2]})
#  ID  Quantity_Lost  Quantity_Draw  Quantity_Won
#0  a              3            2.0             5
#1  b              5            2.0             1
#2  c              3            1.0             4
#3  d              4            NaN             2
df1 = df[df.filter(regex='Quantity_').sum(axis=1).ge(7)]
df1 = pd.wide_to_long(df1, stubnames=['Quantity'], i='ID', j='Result', sep='_', suffix='.*')
sns.barplot(x='ID', y='Quantity', hue='Result', data=df1.reset_index(), palette=['tomato','yellow','limegreen'])

enter image description here


但是,使用熊猫进行绘图要容易得多:

df[df.filter(regex='Quantity_').sum(axis=1).ge(7)].plot.bar(color=['tomato','yellow','limegreen'])

相关问题 更多 >