简体   繁体   English

有没有一种简单的方法可以在 Seaborn 的同一个 FacetGrid 上 plot 两个分类图?

[英]Is there an easy way to plot two categorical plots on the same FacetGrid in Seaborn?

I am attempting to use Seaborn's catplot functionality to plot barplots, faceted by two features.我正在尝试将 Seaborn 的 catplot 功能用于 plot 条形图,具有两个功能。

sns.catplot(kind='bar', data=df, col='col1', y='col2', x='col3')

I now want to add the original data (in the form of a stripplot) on top.我现在想在顶部添加原始数据(以条形图的形式)。 If I were doing this with only one panel, I would just plot them on the same axes:如果我只用一个面板来做这件事,我只会 plot 它们在相同的轴上:

fig, ax = plt.subplots()
sns.barplot(data=df, y='col2', x='col3', ax=ax, alpha=0.5)
sns.stripplot(data=df, y='col2', x='col3', ax=ax, dodge=True)

matplotlib 轴与 barplot 和 stripplot

However, since catplot returns a FacetGrid, I cannot figure out how to do the analogous thing.但是,由于 catplot 返回一个 FacetGrid,我无法弄清楚如何做类似的事情。 Thanks!谢谢!

catplot is a helper function designed to facilitate common faceted plots. catplot是一个帮助器 function 旨在促进常见的刻面图。 If you want more control, you are advised to use FacetGrid directly:如果你想要更多的控制,建议你直接使用FacetGrid

N = 1000
df = pd.DataFrame({'col1':np.random.choice(['A','B','C'], size=N),
                   'col2':np.random.random(size=N),
                   'col3':np.random.choice(['D','E'], size=N)})
g = sns.FacetGrid(data=df, col='col1', col_wrap=2)
g.map_dataframe(sns.barplot, x='col3', y='col2', alpha=0.5, ci=None)
g.map_dataframe(sns.stripplot, x='col3', y='col2')

在此处输入图像描述

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

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