How to create count plot using Seaborn

Seaborn is a popular Python-based data analysis toolkit that can be imported using:

import seaborn as sns

Seaborn can aid in the creation of multiple types of data analysis graphs. One such graph is a countplot.

The default implementation of countplot is:

DataFrame.boxplot(x = None y= None, hue= None, data= None, order= None, hue_order= None, orient= None, color= None, palette= None,saturation= 0.75,dodge:bool = True,ax= None,**kwargs)

Parameters

  • x: string, list of string - Name of variable in data or vector data. Input for plotting long-form data.

  • y: string, list of string - Name of variable in data or vector data. Input for plotting long-form data.

  • hue: string, list of string - Name of variable in data or vector data. Input for plotting long-form data.

  • data: DataFrame, array, list of arrays - If x and y are absent this is interpreted as wide-form; else, it is interpreted as long-form.

  • order: list of string - The order in which to plot categorical levels.

  • hue_order: list of string - The order in which to plot categorical levels.

  • orient: “v” or “h” - Orientation of the plot.

  • color: matplotlib color - Color for the palette or seed for gradient palette.

  • palette: palette name, list, dict - Colors to use for the different levels of the hue variable.

  • saturation: float - Proportion of the original saturation to draw colors at.

  • dodge: bool - To shift elements when hue nesting is used.

  • ax: matplotlib Axes - Axes object on which the plot should be drawn.

  • **kwargs: value mappings - Other keyword arguments are passed through to matplotlib.axes.Axes.bar().

Returns

  • ax :matplotlib Axes - Returns the Axes object with the plot drawn onto it.

Code

The following code shows how count plot can be added in Python. You can change different parameters and look at how the output changes.

The following code takes the marks that each student receives for a particular subject and produces a count plot of these marks.

main.py
dataset.csv
from scipy import stats
import pandas as pd
import seaborn as sns
#add dataset
df = pd.read_csv('dataset.csv')
#plot the graph
ax = sns.countplot(x = 'Hist', data = df)

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved