【问题标题】:How to remove empty bars from catplot如何从 catplot 中删除空条
【发布时间】:2021-04-20 05:57:30
【问题描述】:

我有两个疑问:

  1. 我想从条形图中删除空条(显示在第一列中)。
  2. 我必须在 PowerPoint 演示文稿中使用此图表。如何增加条形图的高度以固定幻灯片的高度?我试图增加高度,但它没有进一步增加。可能吗?如果不是,我可以尝试哪些其他选择?

plt.figure(figsize=(40,20))
    g = sns.catplot(x = 'Subject', y = 'EE Score',data = df , hue = 'Session',col='Grade',sharey = True,sharex = True,
                    hue_order=["2017-18", "2018-19", "2019-20"], kind="bar");
    #plt.legend(bbox_to_anchor=(1, 1), loc=2) 
    g.set(ylim=(0, 100))
    g.set_axis_labels("Subject", "EE Score")
    
    ax = g.facet_axis(0,0)
    for p in ax.patches:
        ax.text(p.get_x() + 0.015, 
                p.get_height() * 1.02, 
                '{0:.1f}'.format(p.get_height()), 
                color='black', rotation='horizontal', size=12)
    ax = g.facet_axis(0,1)
    for p in ax.patches:
        ax.text(p.get_x() + 0.015, 
                p.get_height() * 1.02, 
                '{0:.1f}'.format(p.get_height()), 
                color='black', rotation='horizontal', size=12)
    ax = g.facet_axis(0,2)
    for p in ax.patches:
        ax.text(p.get_x() + 0.015, 
                p.get_height() * 1.02, 
                '{0:.1f}'.format(p.get_height()), 
                color='black', rotation='horizontal', size=12)
    ax = g.facet_axis(0,3)
    for p in ax.patches:
        ax.text(p.get_x() + 0.015, 
                p.get_height() * 1.02, 
                '{0:.1f}'.format(p.get_height()), 
                color='black', rotation='horizontal', size=12)
        
    #g.set_ylabel('')
    plt.savefig('2.png', bbox_inches = 'tight')

【问题讨论】:

  • 不正确;见sharex

标签: python matplotlib seaborn bar-chart catplot


【解决方案1】:

和@JohanC 一样,我最初认为不可能从catplot() 中删除一个空类别。但是,Michael 的评论提供了解决方案:sharex=False

但是请注意颜色是如何在第一类和第二类、第三类之间变化的。如果你想有一致的颜色,你应该传递一个color= 参数。

titanic = sns.load_dataset('titanic')
# remove one category
titanic.drop(titanic.loc[(titanic['class']=='First')&(titanic['who']=='child')].index, inplace=True)
g = sns.catplot(x="who", y="survived", col="class", data=titanic, kind="bar", ci=None, sharex=False)

【讨论】:

  • 也许值得做data=titanic.sort_values("who") 以便条形图在每个方面以相同的顺序出现。您还可以使用 palette 的字典参数来获得具有一致级别的多种颜色 -> 颜色映射。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-13
  • 2018-09-29
  • 2020-04-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多