如何从CatPlot中删除空条

2024-08-20Python开发问题
0

本文介绍了如何从CatPlot中删除空条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有两个问题:

  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')

推荐答案

和@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)

这篇关于如何从CatPlot中删除空条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10

按10分钟间隔对 pandas 数据帧进行分组
Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)...
2024-08-22 Python开发问题
11