pandas - 使用两列的组合自动绘制图表

塔布拉

在以下情况下自动化图形生成的最佳方法是什么:

  • 我有一个具有不同计划和类型的数据框
  • 我想要每个计划和类型组合的图表

数据框:

plan type  hour  ok    notok  other 
A    cont   0    60.0  40.0    0.0    
A    cont   1    56.6  31.2    12.2    
A    vend   2    30.0  50.0    20.0    
B    test   5    20.0  50.0    30.0     

对于只有一种计划和类型的 df,我编写了以下代码:

fig_ = df.set_index('hour').plot(kind='bar', stacked=True, colormap='YlOrBr')
plt.xlabel('Hour')
plt.ylabel('(%)')
fig_.figure.savefig('p_hour.png', dpi=1000)
plt.show()

最后,我想为每个计划和类型的组合保存一个不同的数字。提前致谢!

普里克斯

您可以尝试使用以下方法迭代组groupby

for (plan, type), group in df.groupby(['plan', 'type']):
    fig_ = group.set_index('hour').plot(kind='bar', stacked=True, colormap='YlOrBr')
    plt.xlabel('Hour')  # Maybe add plan and type
    plt.ylabel('(%)')  # Maybe add plan and type
    fig_.figure.savefig('p_hour_{}_{}.png'.format(plan, type), dpi=1000)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章