简体   繁体   English

使用 seaborn 创建分组条 plot

[英]Create a grouped bar plot using seaborn

I have a dataframe as below我有一个 dataframe 如下

category val1 val2 val3
A       2    3     2
A       3    4     1
B       4    5     2
C       3    3     2
B       4    5     2
C       3    3     2

I am trying to create a grouped bar visual that has category in the x-axis, and val1, val2, val3 as y-axis.我正在尝试创建一个分组条形视觉效果,该视觉效果在 x 轴上具有类别,并且 val1、val2、val3 作为 y 轴。

my code is similar to this:我的代码与此类似:

plt.bar(df['category'], df['var1'])
plt.bar(df['category'], df['var2'])
plt.bar(df['category'], df['var3'])

however, I didn't get a grouped bar graph.但是,我没有得到分组条形图。 It is something like this.是这样的。 Is there anyway to fix this?有没有什么办法解决这一问题?

在此处输入图像描述

I'm not sure this is what you are after, but you can try these two options:我不确定这是否是您所追求的,但您可以尝试以下两个选项:

import pandas as pd
from io import StringIO
import seaborn as sns

data = """category val1 val2 val3
A       2    3     2
A       3    4     1
B       4    5     2
C       3    3     2
B       4    5     2
C       3    3     2"""

df = pd.read_csv(StringIO(data), sep="\s+")

g = sns.barplot(
    data=df.melt(id_vars = ["category"], value_vars=["val1", "val2", "val3"]),
    y="value", x="variable", hue="category", ci=None
)

在此处输入图像描述

or或者

g = sns.catplot(
    data=df.melt(id_vars = ["category"], value_vars=["val1", "val2", "val3"]),
    kind="bar",
    y="value", x="variable", col="category", ci=None
)
g.set_axis_labels("", "")

在此处输入图像描述

The key to these approaches is to use melt to unpivot your data.这些方法的关键是使用melt来反透视您的数据。

Note also that the above is not dealing with your duplicate categories.另请注意,上述内容不涉及您的重复类别。 If you want your values to be unique, you can group your df by category and aggregate values before plotting.如果您希望您的值是唯一的,您可以在绘图之前按category和聚合值对您的df进行分组。

Your categories aren't unique.您的类别不是唯一的。 What do you want to see on the x-axis?你想在 x 轴上看到什么?

Suppose they were unique, then you could simply do:假设它们是独一无二的,那么您可以简单地执行以下操作:

import pandas as pd
import seaborn as sns
sns.set()

df = pd.DataFrame()
df['category'] = ['A','B','C','D','E','F']
df['val1'] = [2,3,4,3,4,3]
df['val2'] = [3,4,5,3,5,3]
df['val3'] = [2,1,2,2,2,2]

df.set_index('category').plot(kind='bar', stacked=True)

在此处输入图像描述


Edit: seaborn doesn't support stacked bar charts natively, but here's a hacky way if you need to (or if others are looking for what's actually in the title).编辑: seaborn本身不支持堆叠条形图,但如果您需要(或者如果其他人正在寻找标题中的实际内容),这是一种 hacky 方式。

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()

df = pd.DataFrame()
df['category'] = ['A','B','C','D','E','F']
df['val1'] = [2,3,4,3,4,3]
df['val2'] = [3,4,5,3,5,3]
df['val3'] = [2,1,2,2,2,2]

# create total columns
df['v1+v2+v3'] = df.val1 + df.val2 + df.val3
df['v1+v2'] = df.val1 + df.val2

# plot total
g = sns.barplot(x=df['category'], y=df['v1+v2+v3'], color='green', label='val3')

# plot middle values
g = sns.barplot(x=df['category'], y=df['v1+v2'], color='orange', label='val2')

# plot bottom values
g = sns.barplot(x=df['category'], y=df['val1'], color='blue', label='val1')

# rename axes
g.set_ylabel('vals')
g.set_xlabel('categories')

plt.legend()

在此处输入图像描述

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

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