devarena logo
Reading Time: 4 minutes

Love it or hate it, barplots are often useful in a quick exploratory data analysis to understand the variables in a dataset. In this post, we will see multiple examples on how to make barplots/countplot using Seaborn’s catplot() function. A couple of years ago Seaborn introduced catplot() function that provides a common framework to make most common plots involving categorical and numerical variables.

Seaborn’s catplot() function gives

access to several axes-level functions that show the relationship between a numerical and one or more categorical variables using one of several visual representations.

First, let us load the packages needed.

import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt

We will use taxis dataset from NYC Taxi and Limousine Commission (TLC) available on Seaborn.

taxis = sns.load_dataset("taxis")

It is a big dataset containing ride details of over 6000 rides in NYC.

taxis.columns

Index(['pickup', 'dropoff', 'passengers', 'distance', 'fare', 'tip', 'tolls',
       'total', 'color', 'payment', 'pickup_zone', 'dropoff_zone',
       'pickup_borough', 'dropoff_borough'],
      dtype="object")
taxis.shape

(6433, 14)

Barplot with Seaborn’s catplot()

Barplots show the relationship between between a numeric and a categorical variable. Typically, the values of categorical variables will be on the x-axis and the bar height represent the numerical value corresponding to each value of the categorical variable.
Let us use the number of passengers travelled in a taxi as our categorical variable and the frequency count as numerical variable.

We can use Pandas value_counts() get counts for each unique value of the categorical variable.

df = taxis['passengers'].value_counts().reset_index()
df.columns = ["passengers", "count"]

Here is how our data looks like.

df

passengers	count
0	1	4678
1	2	876
2	5	277
3	3	243
4	6	153
5	4	110
6	0	96

We can use Seaborn’s catplot() function with kind=”bar” argument to make the barplot. Make sure we describe each argument with its name. Here we also control the size of the barplot image using height and aspect arguments.

sns.catplot(x="passengers",
            y="count",
            kind="bar", 
            data=df,
            height=5,
            aspect=1.5)
plt.xlabel("Number of Passengers Travelled", size=14)
plt.ylabel("Count", size=14)
plt.title("Seaborn Barplot Example", size=18)
plt.tight_layout()
plt.savefig("Seaborn_barplot_with_catplot.png")

Here is how the barplot looks like. We can quickly see that, single traveler taxi hire is most common among all the rides.

Seaborn Barplot with Catplot

How to Reorder Bars in Barplot made with Seaborn catplot()

To order the bars in the barplot in ascending or descending order, we specify the order of the bars by using “order” argument. In this example, we are ordering the bars in descending order as our dataframe is already in descending order.

sns.catplot(x="passengers",
            y="count",
            kind="bar", 
            order = df['passengers'],
            data=df,
            height=5,
            aspect=1.5)
plt.xlabel("Number of Passengers Travelled", size=14)
plt.ylabel("Count", size=14)
plt.title("Seaborn Barplot Example: Decending order", size=18)
plt.tight_layout()
plt.savefig("Seaborn_barplot_with_reordering_bars_catplot.png")

How to Reorder Bar in a Barplot made with Seaborn Catplot
How to Reorder Bar in a Barplot made with Seaborn Catplot

To order the bars in ascending order, we simply reverse the order argument. In this example, order=reversed(df[‘passengers’]).

Seaborn Countplot with Seaborn catplot

In the previous example, our values on y-axis is counts of the values of the categorical variable. To make a barplot with counts, another easy option we have is to use countplot specifying the categorical variable alone. This saves the step of us using value_counts() to get the number observations for each value of the categorical variable.
We need to specify kind=”count” to Seaborn’s catplot() function and just x-axis variable.

sns.catplot(x="passengers", 
            kind="count", 
            data=taxis,
            height=5,
            aspect=1.5)
plt.xlabel("Number of Passengers Travelled", size=14)
plt.ylabel("Count", size=14)
plt.title("Seaborn Countplot Example", size=18)
plt.tight_layout()
plt.savefig("Seaborn_countplot_with_catplot.png")

And we get the same barplot as before.

Seaborn Count plot with catplot()
Seaborn Count plot with catplot()

To reorder the bars in the countplot, we use order argument and this time we need to get the right order we need. In this example we are using value_counts() function to get the order of the values in descending order.

sns.catplot(x="passengers", 
            kind="count", 
            order = taxis['passengers'].value_counts().index,
            data=taxis,
            height=5,
            aspect=1.5)
plt.xlabel("Number of Passengers Travelled", size=14)
plt.ylabel("Count", size=14)
plt.title("Seaborn Countplot Example", size=18)
plt.tight_layout()
plt.savefig("Seaborn_countplot_with_catplot_reordering_bars_in_decending_order.png")

Seaborn countplot with bars in descending order
Seaborn countplot: Reordering bars in descending order

Source link

Spread the Word!