Open In App

Grouped Barplots in Python with Seaborn

Prerequisites: Seaborn

In this article, we will discuss ways to make grouped barplots using Seaborn in Python. Before that, there are some concepts one must be familiar with:



Procedure

Following are implementation to explain the same:
 

Example 1:






# importing packages
import seaborn as sb
  
# load dataset
df = sb.load_dataset('tips')
  
# perform groupby
df = df.groupby(['size', 'sex']).agg(mean_total_bill=("total_bill", 'mean'))
df = df.reset_index()
  
# plot barplot
sb.barplot(x="size",
           y="mean_total_bill",
           hue="sex",
           data=df)

Output:

Example 2:




# importing packages
import seaborn as sb
  
# load dataset
df = sb.load_dataset('tips')
  
# perform groupby
df = df.groupby(['size', 'day']).agg(mean_total_bill=("total_bill", 'mean'))
df = df.reset_index()
  
# plot barplot
sb.barplot(x="size",
           y="mean_total_bill",
           hue="day",
           data=df)

Output:

Example 3:




# importing packages
import seaborn as sb
  
# load dataset
df = sb.load_dataset('anagrams')
  
# perform groupby
df = df.groupby(['num1', 'attnr']).agg(mean_num3=("num3", 'mean'))
df = df.reset_index()
  
# plot barplot
sb.barplot(x="num1",
           y="mean_num3",
           hue="attnr",
           data=df)

Output:


Article Tags :