0

I want to make a grouped bar chart in seaborn with the following data. How do I do this? The seaborn documentation has a "hue" parameter but how can I use this with multiple columns I want to display?

Type        Single Double Many
McDonalds        1      2   10
BK               4      8    7
Popeyes         11      4   11

Heres an example of what I've done this using matplotlib...want to do this in seaborn though

df.plot.bar(figsize = (20,10), xlabel = 'Type', ylabel = 'Counts', title = 'Type Counts', rot = 0)
Eisen
  • 1,697
  • 9
  • 27

1 Answers1

1

If you convert the data to vertical format and set the original column name to hue, you will get a graph that is grouped by type.

df.set_index('Type', inplace=True)
df = df.stack().to_frame('value').reset_index()
df.rename(columns={'level_1':'Item'},inplace=True)

df.head()

Type    Item    value
0   McDonalds   Single  1
1   McDonalds   Double  2
2   McDonalds   Many    10
3   BK  Single  4
4   BK  Double  8

import seaborn as sns

sns.barplot(data=df, x='Type', y='value', hue='Item')

enter image description here

r-beginners
  • 31,170
  • 3
  • 14
  • 32