简体   繁体   English

从分组的条形图中删除空条

[英]Remove empty bars from grouped barplot

I have a grouped barplot. 我有一个分组的barplot。 It's working very well, but I try to remove the empty barplots. 它工作得很好,但是我尝试删除空的条形图。 They take too much space. 它们占用太多空间。

I have already tried : 我已经尝试过:

%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. 我不知道您要完成什么,但是从您所说的来看,我猜您正在尝试不显示空的数据透视对。

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" . 组图需要显示所有它们,即使是NaNs也将显示为“空条”

Furthermore after groupby every group is at least size of one, so df_2[df_2['count'] != 0] is allways true. 此外, groupby之后每个组的大小至少为1,因此df_2[df_2['count'] != 0]始终为true。

For example 例如

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

gives

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

and

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

shows 表演

在此处输入图片说明

And that's the way it is. 就是这样。 Plot need to show all groups after pivot. 绘制后需要显示所有组。

EDIT 编辑

You can also use stacked plot, to get rid of spaces 您还可以使用堆积图来摆脱空间

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

叠放

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM