Python:按条件绘制多个正/负条形图 [英] Python: draw multiple positive/negative Bar Charts by conditions

查看:63
本文介绍了Python:按条件绘制多个正/负条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次用python绘制条形图.

我的df操作:

 键描述分数0面条味道51面条颜色-22面条健康33苹果色74苹果硬9 

我的代码:

 将matplotlib.pyplot导入为pltop ['positive'] = op ['score']>0op ['score'].plot(kind ='barh',color = op.positive.map({True:'r',False:'k'}),use_index = True)plt.show()plt.savefig('sample1.png') 

输出:

但这不是我所期望的.在这种情况下,我想用索引不同的键绘制两个图表,并可能使用如下所示的不同颜色:

我该怎么做?

解决方案

尝试:

  fig,ax = plt.subplots(1,op.key.nunique(),figsize =(15,5),sharex = True)我= 0#修复一些数据问题/打字错误op ['key'] = op.key.str.replace('noodels','noodles')对于n,g在op.assign(positive = op ['score']> = 0).groupby('key')中:g.plot.barh(y ='score',x ='descript',ax = ax [i],color = g ['positive'].map({True:'red',False:'blue'}),legend = False)\.set_xlabel(n)ax [i] .set_ylabel('Score')ax [i] .spines ['top'].set_visible(False)ax [i] .spines ['right'].set_visible(False)ax [i] .spines ['top'].set_visible(False)ax [i] .spines ['left'].set_position('zero')我+ = 1 

输出:

更新为yaxis添加了标签移动-感谢@importanceOfBeingErnest的

This is my first time drawing bar charts in python.

My df op:

      key descript  score
0  noodles    taste      5
1  noodles    color     -2
2  noodles   health      3
3   apple    color      7
4   apple    hard      9

My code:

import matplotlib.pyplot as plt
op['positive'] = op['score'] > 0
op['score'].plot(kind='barh', color=op.positive.map({True: 'r', False: 'k'}), use_index=True)
plt.show()
plt.savefig('sample1.png')

Output:

But this is not what I expected. I would like to draw two charts by different keys in this case with index and maybe use different colors like below:

How can I accomplish this?

解决方案

Try:

fig, ax = plt.subplots(1,op.key.nunique(), figsize=(15,5), sharex=True)
i = 0

#Fix some data issues/typos
op['key']=op.key.str.replace('noodels','noodles')

for n, g in op.assign(positive=op['score'] >= 0).groupby('key'):
    g.plot.barh(y='score', x='descript', ax=ax[i], color=g['positive'].map({True:'red',False:'blue'}), legend=False)\
    .set_xlabel(n)
    ax[i].set_ylabel('Score')
    ax[i].spines['top'].set_visible(False)
    ax[i].spines['right'].set_visible(False)
    ax[i].spines['top'].set_visible(False)
    ax[i].spines['left'].set_position('zero')
    i += 1

Output:

Update added moving of labels for yaxis - Thanks to this SO solution by @ ImportanceOfBeingErnest

fig, ax = plt.subplots(1,op.key.nunique(), figsize=(15,5), sharex=True)
i = 0

#Fix some data issues/typos
op['key']=op.key.str.replace('noodels','noodles')

for n, g in op.assign(positive=op['score'] >= 0).groupby('key'):
    g.plot.barh(y='score', x='descript', ax=ax[i], color=g['positive'].map({True:'red',False:'blue'}), legend=False)\
    .set_xlabel(n)
    ax[i].set_ylabel('Score')
    ax[i].spines['top'].set_visible(False)
    ax[i].spines['right'].set_visible(False)
    ax[i].spines['top'].set_visible(False)
    ax[i].spines['left'].set_position('zero')
    plt.setp(ax[i].get_yticklabels(), transform=ax[i].get_yaxis_transform())
    i += 1

Output:

这篇关于Python:按条件绘制多个正/负条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆