简体   繁体   English

如何将一个分类列的色调添加到 Seaborn 条形图(每列的平均值和标准)?

[英]How to add a hue of one categorical column to Seaborn barplot (of each column mean and std)?

sns.barplot(data = df, ci='sd') gives a barplot of each column mean with std like below. sns.barplot(data = df, ci='sd')给出每列均值的条形图,标准如下所示。

条形图

Let's say there is one column named 'category', which has 3 kinds of values A, B, and C.假设有一列名为“类别”,其中有 3 种值 A、B 和 C。

I want to add a hue parameter to the barplot from this column in order to split each bar of column mean into three according to the category.我想从该列向条形图添加一个色调参数,以便根据类别将每个列均值条分成三个。 Like this:像这样:

预期的

If I directly add hue = 'category' , I got an error Cannot use `hue` without `x` and `y` .如果我直接添加hue = 'category' ,则会出现错误Cannot use `hue` without `x` and `y`

What should x and y be here? x 和 y 应该在这里?

According to the docs, it looks like if you don't pass x or y , then your DataFrame will be interpreted as 'wide-form', and each numeric column will be plotted.根据文档,看起来如果您不通过xy ,那么您的 DataFrame 将被解释为“宽格式”,并且每个数字列都将被绘制。 Then if you want to maintain the plot layout you have, but use hue='category' , you'll need to convert your DataFrame to 'long-form' using melt , using the former columns -- now the var_name column -- as x .然后,如果您想维护您拥有的 plot 布局,但使用hue='category' ,您需要使用之前的列 - 现在是var_name列 - 将 DataFrame 转换为 'long-form' 使用melt x Without the specifics, I can't say exactly which columns you'll need to use, but it should look roughly like this:如果没有具体细节,我无法准确说明您需要使用哪些列,但它应该大致如下所示:

melted_df = df.melt(var_name='variable', value_name='value')
sns.barplot(melted_df, x='variable', y='value', hue='category')

Does this work for you?这对你有用吗?

Note that in general, Seaborn expects your data in long format.请注意,通常,Seaborn 期望您的数据为长格式。 I think you'll find it makes most operations more convenient, as each "unit" of the plot is expressed as a group in a groupby, which is simpler than dealing with grouping along both the row and column axes.我想你会发现它使大多数操作更加方便,因为 plot 的每个“单元”都表示为 groupby 中的一个组,这比处理沿行轴和列轴进行分组更简单。

If you are ok with separate subplots for each column, one way of doing this would be like:如果您对每列的单独子图没问题,那么这样做的一种方法是:

fig, ax = plt.subplots(1, len(df.columns), figsize=(120,6))
for i in range(len(df.columns)):
    sns.barplot(x='category', y=df.columns[i], ax=ax[i], data=df, ci='sd')

Does this work for you?这对你有用吗?

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

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