• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Python / Barplots and Countplot with Seaborn’s catplot

Barplots and Countplot with Seaborn’s catplot

January 20, 2022 by cmdlinetips

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

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Catplot: Boxplot with jitter SeabornCatplot Python Seaborn: One Function to Rule All Plots With Categorical Variables Default Thumbnail8 tips to make better barplots with ggplot2 in R Default ThumbnailAltair 4.0 is here: Barplots, Scatter Plots with Regression Line and Boxplots Seaborn Version 0.11.0 is HereSeaborn Version 0.11.0 is here with displot, histplot and ecdfplot

Filed Under: Python, Python Tips, Seaborn Tagged With: Seaborn barplot, Seaborn Catplot, Seaborn countplot

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version