python - python facetgrid with sns.barplot and map; target no overlapping group bars
问题描述
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()
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']}
)
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)
解决方案
this answer shows up how to use FacetGrid directly.
But, if you have 0.9.0 installed, I would recommend you make use of the new catplot()
function that will produce the right (at least I think?) plot. Note that this function returns a FacetGrid
object. You can pass kwargs to the call to customize the resulting FacetGrid
, or modify its properties afterwards.
g = sns.catplot(data=data, x='A', y='B', hue='type', col='C', kind='bar')
推荐阅读
- python - 是否可以像嵌套函数一样在类中使用导入?
- cuda - 用于访问文件的 cuDoubleComplex 变量的格式说明符
- reactjs - 使用 React 和 twin.macro/emotion/styled-components 开发时更快的渲染
- javascript - 我无法在 React 的 img 标签的 src 属性中使用数组元素
- codenameone - 在 NetBeans 中安装 Codename One 插件时出现问题
- sqlpackage - Azure 上的 SQLPackage 和 DB
- swift - sectionHeaderTopPadding 不适用于 Grouped 和 InsetGrouped UITableView
- amazon-web-services - AWS SSO 自定义应用程序 SAML 身份验证错误(InvalidIdentityToken、Invalid Base64 SAMLResponse
- html - 子元素不与可滚动父元素重叠
- vba - Visio VBA 将形状的“开始”和“结束”作为与水平标尺相关的值返回