带有 sns.barplot 和地图的 python facetgrid;目标没有重叠的组栏 [英] python facetgrid with sns.barplot and map; target no overlapping group bars

查看:79
本文介绍了带有 sns.barplot 和地图的 python facetgrid;目标没有重叠的组栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在分别为带有两个不同组(类型")的条形图子图实现 facetgrid 代码.我打算得到一个情节,其中不同的组不堆叠也不重叠.我正在使用以下代码

I am currently implementing a code for facetgrid with subplots of barplots with two different groups ('type'), respectively. I am intending to get a plot, where the different groups are not stacked and not overlapping. I am using following code

g = sns.FacetGrid(data,
            col='C',
            hue = 'type',
            sharex=False,
            sharey=False,
            size=7,
            palette=sns.color_palette(['red','green']),
            )
g = g.map(sns.barplot, 'A', 'B').add_legend()

数据是一个pandas长格式df,具有以下示例结构:

The data is a pandas long format df with following example structure:

data=pd.DataFrame({'A':['X','X','Y','Y','X','X','Y','Y'],
               'B':[0,1,2,3,4,5,6,7],
               'C':[1,1,1,1,2,2,2,2],
               'type':['ctrl','cond1','ctrl','cond1','ctrl','cond1','ctrl','cond1']}
                )

在创建的条形图中,我现在得到了两个组的完全重叠的条形图,因此缺少 ctrlis,见下文.但是,我打算获得相邻的非重叠条.如何做到这一点?我的真实代码每个图都有更多条,您可以在其中看到重叠的颜色(此处完全覆盖)

In the created barplots I get now fully overlapping barplots of the two groups, thus ctrlis missing, see below. However, I am intending to get neighbouring non-overlapping bars each. How to achieve that? My real code has some more bars per plot, where you can see overlapping colors (here fully covered)

推荐答案

我认为您希望为条形图提供 hue 参数,而不是 FacetGrid.因为分组发生在(单个)条形图中,而不是在构面级别.

I think you want to provide the hue argument to the barplot, not the FacetGrid. Because the grouping takes place within the (single) barplot, not on the facet's level.

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns

data=pd.DataFrame({'A':['X','X','Y','Y','X','X','Y','Y'],
               'B':[0,1,2,3,4,5,6,7],
               'C':[1,1,1,1,2,2,2,2],
               'type':['ctrl','cond1','ctrl','cond1','ctrl','cond1','ctrl','cond1']})

g = sns.FacetGrid(data,
            col='C',
            sharex=False,
            sharey=False,
            height=4)
g = g.map(sns.barplot, 'A', 'B', "type", 
          hue_order=np.unique(data["type"]), 
          order=["X", "Y"], 
          palette=sns.color_palette(['red','green']))
g.add_legend()

plt.show()

这篇关于带有 sns.barplot 和地图的 python facetgrid;目标没有重叠的组栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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