How to Create a Bar Plot in Seaborn

2021-01-22

Intro

In a previous post, we looked at how to create a count plot to view a distribution of a cateogircal variable by count. However, sometimes we want to view a cateogirical variable by other measures.

For example, let's say we want to see the total comic book sales by super hero. In stead of seeing how many comics of each, we want to see the amount (maybe a iron man comic cost more than a green latern). In this case, we would sum up the total sales by super hero and use a Bar Plot.

In this article, we will see how to create a bar plot using the Seaborn library.

Creating a Bar Plot

To create a bar plot in Seaborn we use the barplot method. Unlike a count plot, we supply an x named parameter and a y named parameter. The x will be the groups and the y will be the property we are aggregating (summing for example). We also need to specify a dataframe for the data named parameter. In the example below we use the titanic dataset. We group the total fare costs by the who (man, women or child).

import seaborn as sns
 
df = sns.load_dataset('titanic')
 
sns.barplot(x = 'who',
            y = 'fare',
            data = df)
GoTea - KoalaTea