简体   繁体   中英

Create a grouped bar plot using seaborn

I have a dataframe as below

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.

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.

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.

Your categories aren't unique. What do you want to see on the x-axis?

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).

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()

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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