从分组条形图中删除空条 [英] Remove empty bars from grouped barplot

查看:51
本文介绍了从分组条形图中删除空条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个分组的barplot.它运行良好,但我尝试删除空的条形图.它们占用太多空间.

I have a grouped barplot. It's working very well, but I try to remove the empty barplots. They take too much space.

我已经试过了:

%matplotlib inline
import matplotlib as mpl
from matplotlib.gridspec import GridSpec
import matplotlib.pyplot as plt
import sys
import os
import glob
import seaborn as sns
import pandas as pd
import ggplot
from ggplot import aes

sns.set(style= "whitegrid", palette="pastel", color_codes=True )

tab_folder = 'myData'
out_folder ='myData/plots'
tab = glob.glob('%s/R*.tab'%(tab_folder))

#is reading all my data
for i, tab_file in enumerate(tab):
    folder,file_name=os.path.split(tab_file)
    s_id=file_name[:-4].replace('DD','')
    df=pd.DataFrame.from_csv(tab_file, sep='\t')

    df_2 = df.groupby(['name','ab']).size().reset_index(name='count')

    df_2 = df_2[df_2['count'] != 0]

    table = pd.pivot_table(df_2, index='name',columns='ab', values='count' ) 
    table.plot(kind='barh', width = 0.9, color = ['b', 'g', 'r'], ax = ax)

    for label in (ax.get_xticklabels() + ax.get_yticklabels()):

        label.set_fontsize(4)


    ax.set_title(s_id).update({'color':'black', 'size':5, 'family':'monospace'})
    ax.set_xlabel('')
    ax.set_ylabel('')

    handles, labels = ax.get_legend_handles_labels()
    ax.legend(handles[::-1], labels[::-1], bbox_to_anchor=(1, 1.05),prop= {'size': 4} )

png_t = '%s/%s.b.png'%(out_folder,s_id)
plt.savefig(png_t, dpi = 500)

但它不起作用.条形仍然相同.有没有其他方法可以去除空条?

But it's not working. The bars are still the same. Is there any other method to remove empty bars?

推荐答案

您的问题不完整.我不知道您要完成什么,但是根据您所说的,我想您是在尝试不显示空的数据透视对.

Your question is not complete. I don't know what you're trying to accomplish, but from what you've said I'd guess that you are trying not to display empty pivot pairs.

这是不可能通过标准方式实现的.组的图需要显示所有它们,甚至是 NaNs ,它们将被绘制为空条" .

This is not possible by standard means of pandas. Plot of groups need to display all of them even NaNs which will be plot as "empty bars".

此外,在 groupby 之后,每个组的大小至少为 1,因此 df_2[df_2['count'] != 0] 始终为真.

Furthermore after groupby every group is at least size of one, so df_2[df_2['count'] != 0] is allways true.

例如

df = pd.DataFrame([['nameA', 'abA'], ['nameB', 'abA'],['nameA','abB'],['nameD', 'abD']], columns=['names', 'ab'])
df_2 = df.groupby(['names', 'ab']).size().reset_index(name='count')
df_2 = df_2[df_2['count'] != 0] # this line has no effect
table = pd.pivot_table(df_2, index='names',columns='ab', values='count' ) 
table

给予

ab      abA     abB     abD
names           
nameA   1.00    1.00    NaN
nameB   1.00    NaN     NaN
nameD   NaN     NaN     1.00

table.plot(kind='barh', width = 0.9, color = ['b', 'g', 'r'])

节目

事情就是这样.绘制后需要显示所有组.

And that's the way it is. Plot need to show all groups after pivot.

编辑

您也可以使用堆叠图来消除空格

You can also use stacked plot, to get rid of spaces

table.plot(kind='barh', width = 0.9, color = ['b', 'g', 'r'], stacked=True)

这篇关于从分组条形图中删除空条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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