The Wayback Machine - https://web.archive.org/web/20240827050235/https://www.geeksforgeeks.org/countplot-using-seaborn-in-python/
Open In App

Countplot using seaborn in Python

Last Updated : 12 Jun, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Seaborn is an amazing visualization library for statistical graphics plotting in Python. It provides beautiful default styles and color palettes to make statistical plots more attractive. It is built on the top of matplotlib library and also closely integrated to the data structures from pandas.
 

Seaborn.countplot()

seaborn.countplot() method is used to Show the counts of observations in each categorical bin using bars.
 

Syntax : seaborn.countplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, orient=None, color=None, palette=None, saturation=0.75, dodge=True, ax=None, **kwargs)
Parameters : This method is accepting the following parameters that are described below: 
 

  • x, y: This parameter take names of variables in data or vector data, optional, Inputs for plotting long-form data.
  • hue : (optional) This parameter take column name for colour encoding.
  • data : (optional) This parameter take DataFrame, array, or list of arrays, Dataset for plotting. If x and y are absent, this is interpreted as wide-form. Otherwise it is expected to be long-form.
  • order, hue_order : (optional) This parameter take lists of strings. Order to plot the categorical levels in, otherwise the levels are inferred from the data objects.
  • orient : (optional)This parameter take “v” | “h”, Orientation of the plot (vertical or horizontal). This is usually inferred from the dtype of the input variables but can be used to specify when the “categorical” variable is a numeric or when plotting wide-form data.
  • color : (optional) This parameter take matplotlib color, Color for all of the elements, or seed for a gradient palette.
  • palette : (optional) This parameter take palette name, list, or dict, Colors to use for the different levels of the hue variable. Should be something that can be interpreted by color_palette(), or a dictionary mapping hue levels to matplotlib colors.
  • saturation : (optional) This parameter take float value, Proportion of the original saturation to draw colors at. Large patches often look better with slightly desaturated colors, but set this to 1 if you want the plot colors to perfectly match the input color spec.
  • dodge : (optional) This parameter take bool value, When hue nesting is used, whether elements should be shifted along the categorical axis.
  • ax : (optional) This parameter take matplotlib Axes, Axes object to draw the plot onto, otherwise uses the current Axes.
  • kwargs : This parameter take key, value mappings, Other keyword arguments are passed through to matplotlib.axes.Axes.bar().

Returns: Returns the Axes object with the plot drawn onto it.
 

The below examples illustrate the countplot() method of the seaborn library.

Grouping variables in Seaborn countplot with different attributes

Example 1: Show value counts for a single categorical variable. 

If we use only one data variable instead of two data variables then it means that the axis denotes each of these data variables as an axis.

X denotes an x-axis and y denote a y-axis.

Syntax: 

seaborn.countplot(x)

Python3




# importing the required library
 
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a tips.csv file from seaborn library
df = sns.load_dataset('tips')
 
# count plot on single categorical variable
sns.countplot(x ='sex', data = df)
 
# Show the plot
plt.show()


Output : 
 

Countplot-1

Example 2 : Show value counts for two categorical variables and using hue parameter:

While the points are plotted in two dimensions, another dimension can be added to the plot by coloring the points according to a third variable.

Syntax:

seaborn.countplot(x, y, hue, data);

Python3




# importing the required library
 
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a tips.csv file from seaborn library
df = sns.load_dataset('tips')
 
# count plot on two categorical variable
sns.countplot(x ='sex', hue = "smoker", data = df)
 
# Show the plot
plt.show()


Output : 
 

countplot-2

Example 3 : Plot the bars horizontally. 

In the above example, we see how to plot a single horizontal countplot and here can perform multiple horizontal count plots with the exchange of the data variable with another axis.
 

Python3




# importing the required library
 
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a tips.csv file from seaborn library
df = sns.load_dataset('tips')
 
# count plot along y axis
sns.countplot(y ='sex', hue = "smoker", data = df)
 
# Show the plot
plt.show()


Output : 
 

countplot-3

Example 4 : Use different color palette attributes. 

Using the palette we can generate the point with different colors. In this below example we can see the palette can be responsible for a generate the countplot with different colormap values.

Syntax:

seaborn.countplot(x, y, data, hue, palette)
 

Python3




# importing the required library
 
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a tips.csv file from seaborn library
df = sns.load_dataset('tips')
 
# use a different colour palette in count plot
sns.countplot(x ='sex', data = df, palette = "Set2")
 
# Show the plot
plt.show()


Output : 
 

countplot-4

Possible values of palette are:

Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r, CMRmap, CMRmap_r, Dark2, Dark2_r,

GnBu, GnBu_r, Greens, Greens_r, Greys, Greys_r, OrRd, OrRd_r, Oranges, Oranges_r, PRGn, PRGn_r, Paired, Paired_r,

Pastel1, Pastel1_r, Pastel2, Pastel2_r, PiYG, PiYG_r, PuBu, PuBuGn, PuBuGn_r, PuBu_r, PuOr, PuOr_r, PuRd, PuRd_r,

Purples, Purples_r, RdBu, RdBu_r, RdGy, RdGy_r, RdPu, RdPu_r, RdYlBu, RdYlBu_r, RdYlGn, RdYlGn_r, Reds, Reds_r, Set1,

Set1_r, Set2, Set2_r, Set3, Set3_r, Spectral, Spectral_r, Wistia, Wistia_r, YlGn, YlGnBu, YlGnBu_r, YlGn_r, YlOrBr,

YlOrBr_r, YlOrRd, YlOrRd_r, afmhot, afmhot_r, autumn, autumn_r, binary, binary_r, bone, bone_r, brg, brg_r, bwr, bwr_r,

cividis, cividis_r, cool, cool_r, coolwarm, coolwarm_r, copper, copper_r, cubehelix, cubehelix_r, flag, flag_r, gist_earth,

gist_earth_r, gist_gray, gist_gray_r, gist_heat, gist_heat_r, gist_ncar, gist_ncar_r, gist_rainbow, gist_rainbow_r, gist_stern, 

Example 5: using a color parameter in the plot.

Using color attributes we are Color for all the elements.

Syntax:

seaborn.countplot(x, y, data, color)

Python3




# importing the required library
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a titanic.csv file
# from seaborn library
df = sns.load_dataset('titanic')
 
sns.countplot(x = 'class', y = 'fare',
            hue = 'sex',
            data = df,color="salmon")
 
# Show the plot
plt.show()


Output:

Example 6: Using a saturation parameter in the plot.

Proportion of the original saturation to draw colors at. Large patches often look better with slightly desaturated colors, but set this to 1 if you want the plot colors to perfectly match the input color spec.

Syntax:

seaborn.colorplot(x, y, data, saturation)

Python3




# importing the required library
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a titanic.csv file
# from seaborn library
df = sns.load_dataset('titanic')
 
# class v / s fare barplot
sns.countplot(x ='sex', data = df,
              color="salmon",
              saturation = 0.1)
# Show the plot
plt.show()


Output:

Example 7: Use matplotlib.axes.Axes.bar() parameters to control the style.

We can set Width of the gray lines that frame the plot elements using linewidth. Whenever we increase linewidth than the point also will increase automatically.

Syntax:

seaborn.countplot(x, y, data, linewidth, edgecolor)

Python3




# importing the required library
import seaborn as sns
import matplotlib.pyplot as plt
 
# read a titanic.csv file
# from seaborn library
df = sns.load_dataset('titanic')
 
# class v / s fare barplot
sns.countplot(x ='sex', data = df,color="salmon", facecolor=(0, 0, 0, 0),
                   linewidth=5,
                   edgecolor=sns.color_palette("BrBG", 2))
# Show the plot
plt.show()


Output:

Colormap Possible values are:  

Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r,  

CMRmap, CMRmap_r, Dark2, Dark2_r, GnBu, GnBu_r, Greens, Greens_r, Greys, Greys_r,  

OrRd, OrRd_r, Oranges, Oranges_r, PRGn, PRGn_r, Paired, Paired_r, Pastel1, Pastel1_r,  

Pastel2, Pastel2_r, PiYG, PiYG_r, PuBu, PuBuGn, PuBuGn_r, PuBu_r, PuOr, PuOr_r, PuRd,

PuRd_r, Purples, Purples_r, RdBu, RdBu_r, RdGy, RdGy_r, RdPu, RdPu_r, RdYlBu, RdYlBu_r,

RdYlGn, RdYlGn_r, Reds, Reds_r, Set1, Set1_r, Set2, Set2_r, Set3, Set3_r, Spectral,

 



Previous Article
Next Article

Similar Reads

How to Make Countplot or barplot with Seaborn Catplot?
In Python, we can normally plot barcharts for numerical variables. But when it comes to the case of categorical variables then we cannot normally plot the count for each category. Here comes Seaborn Catplot in the picture. It allows you to plot the count of each category for non-numerical/categorical variables. Creating the Countplot Countplot give
1 min read
How to Show Mean on Boxplot using Seaborn in Python?
A boxplot is a powerful data visualization tool used to understand the distribution of data. It splits the data into quartiles, and summarises it based on five numbers derived from these quartiles: median: the middle value of data. marked as Q2, portrays the 50th percentile.first quartile: the middle value between "minimum non-outlier" and median.
2 min read
Boxplot using Seaborn in Python
Seaborn is an amazing visualization library for statistical graphics plotting in Python. It provides beautiful default styles and color palettes to make statistical plots more attractive. It is built on the top of matplotlib library and also closely integrated into the data structures from pandas. Box Plot A box plot helps to maintain the distribut
5 min read
Grid Plot in Python using Seaborn
Grids are general types of plots that allow you to map plot types to grid rows and columns, which helps you to create similar character-separated plots. In this article, we will be using two different data sets (Iris and Tips) for demonstrating grid plots Using Iris Dataset We are going to use the Iris dataset which is a very famous dataset availab
4 min read
Lineplot using Seaborn in Python
Seaborn is an amazing visualization library for statistical graphics plotting in Python. It provides default styles and color palettes to make statistical plots more attractive. It is built on the top of the matplotlib library and is also closely integrated into the data structures from pandas. LineplotVisual representation of a dataset must be cho
4 min read
Barplot using seaborn in Python
Seaborn is an amazing visualization library for statistical graphics plotting in Python. It provides beautiful default styles and color palettes to make statistical plots more attractive. It is built on the top of matplotlib library and also closely integrated to the data structures from pandas. Seaborn.barplot() seaborn.barplot() method is used to
6 min read
Scatterplot using Seaborn in Python
Seaborn is an amazing visualization library for statistical graphics plotting in Python. It provides beautiful default styles and color palettes to make statistical plots more attractive. It is built on the top of matplotlib library and also closely integrated into the data structures from pandas. Scatter Plot Scatterplot can be used with several s
4 min read
Violinplot using Seaborn in Python
Seaborn is an amazing visualization library for statistical graphics plotting in Python. It provides beautiful default styles and color palettes to make statistical plots more attractive. It is built on the top of matplotlib library and also closely integrated into the data structures from pandas. Violin Plot A violin plot plays a similar activity
7 min read
Swarmplot using Seaborn in Python
Seaborn is an amazing visualization library for statistical graphics plotting in Python. It provides beautiful default styles and color palettes to make statistical plots more attractive. It is built on the top of the matplotlib library and also closely integrated into the data structures from pandas. Swarm Plot Seaborn swarmplot is probably simila
6 min read
Data Visualisation in Python using Matplotlib and Seaborn
It may sometimes seem easier to go through a set of data points and build insights from it but usually this process may not yield good results. There could be a lot of things left undiscovered as a result of this process. Additionally, most of the data sets used in real life are too big to do any analysis manually. This is essentially where data vi
14 min read
Stripplot using Seaborn in Python
Seaborn is an amazing visualization library for statistical graphics plotting in Python. It provides beautiful default styles and color palettes to make statistical plots more attractive. It is built on top of the matplotlib library and also closely integrated into the data structures from pandas. Strip plot A strip plot is drawn on its own. It is
5 min read
Horizontal Boxplots with Points using Seaborn in Python
Seaborn is an amazing visualization library for statistical graphics plotting in Python. It provides beautiful default styles and color palettes to make statistical plots more attractive. It is built on the top of matplotlib library and also closely integrated into the data structures from pandas.Seaborn aims to make visualization of the central pa
3 min read
Python Seaborn - Strip plot illustration using Catplot
Seaborn is a data visualization library that is based on matplotlib. A high-level interface is provided by seaborn for drawing informative and attractive statistical graphics. Seaborn Catplot is a new addition to seaborn that makes plotting easier and involves categorical variables. It is used to show the relation between categorical variables like
2 min read
How To Make Scatter Plot with Regression Line using Seaborn in Python?
In this article, we will learn how to male scatter plots with regression lines using Seaborn in Python. Let's discuss some concepts : Seaborn : Seaborn is a tremendous visualization library for statistical graphics plotting in Python. It provides beautiful default styles and color palettes to make statistical plots more attractive. It is built on t
2 min read
How to Make Boxplots with Data Points using Seaborn in Python?
Prerequisites :SeabornMatplotlib Box Plot or a Whisker Plot is a statistical plot to visualize graphically, depicting group of numerical data through their quartiles. This plot displays the summary of set of data containing the five values known as minimum, quartile 1, quartile 2 or median, quartile 3 and maximum, where the box is drawn from first
3 min read
How to add text in a heatmap cell annotations using seaborn in Python ?
Prerequisite: Seaborn heatmap Heatmap is defined as a graphical representation of data using colors to visualize the value of the matrix. In this, to represent more common values or higher activities brighter colors basically reddish colors are used and to represent less common or activity values, darker colors are preferred. Heatmap is also define
3 min read
Make Violinplot with data points using Seaborn
A violin plot plays a similar activity that is pursued through whisker or box plot do. As it shows several quantitative data across one or more categorical variables. It can be an effective and attractive way to show multiple data at several units. A “wide-form” Data Frame helps to maintain each numeric column which can be plotted on the graph. It
2 min read
Plotting different types of plots using Factor plot in seaborn
Prerequisites: Introduction to Seaborn Seaborn is an amazing visualization library for statistical graphics plotting in Python. It provides beautiful default styles and color palettes to make statistical plots more attractive. It is built on the top of matplotlib library and also closely integrated to the data structures from pandas. Factor Plot Fa
3 min read
How To Create A Multiline Plot Using Seaborn?
Data visualization is a crucial component of data analysis, and plotting is one of the best ways to visualize data. The Python data visualization package Seaborn offers a high-level interface for making visually appealing and educational statistics visuals. The multiline plot, which lets you see numerous lines on one plot and makes it simple to com
4 min read
How To Invert Axis Using Seaborn Objects Interface?
Seaborn, a popular Python data visualization library built on top of Matplotlib, offers an intuitive interface for creating appealing statistical graphics. One of the frequently used features in data visualization is the ability to invert axes, which can provide a different perspective on the data being visualized. In this article, we'll explore th
4 min read
Using the Hue Parameter in Histograms with Seaborn
Seaborn is a powerful Python library for data visualization, built on top of Matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics. One of the most versatile functions in Seaborn is histplot, which allows you to create histograms to visualize the distribution of datasets. A particularly useful fe
5 min read
Plotting Multiple Figures in a Row Using Seaborn
Seaborn is a powerful Python library for data visualization based on Matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics. In this article, we'll explore how to plot multiple figures in a row using Seaborn. This can be particularly useful when comparing different datasets or visualizations side
5 min read
Plotting Numpy Array Using Seaborn
Seaborn, a powerful Python data visualization library built on top of matplotlib, provides a range of tools for creating informative and attractive statistical graphics. One of its key features is the ability to plot numpy arrays, which are fundamental data structures in Python. This article delves into the details of plotting numpy arrays using Se
4 min read
Horizontal Boxplots with Seaborn in Python
Prerequisite: seaborn The Boxplots are used to visualize the distribution of data which is useful when a comparison of data is required. Sometimes, Boxplot is also known as a box-and-whisker plot. The box shows the quartiles of dataset and whiskers extend to show rest of the distribution. In this article, we are going to implement the Horizontal bo
1 min read
Python Seaborn - Catplot
Seaborn is a Python data visualization library based on matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics. Seaborn helps resolve the two major problems faced by Matplotlib; the problems are? Default Matplotlib parametersWorking with data frames As Seaborn compliments and extends Matplotlib, t
5 min read
Python - seaborn.jointplot() method
Seaborn is a Python data visualization library based on matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics. Seaborn helps resolve the two major problems faced by Matplotlib; the problems are ? Default Matplotlib parametersWorking with data frames As Seaborn compliments and extends Matplotlib,
3 min read
Python - seaborn.factorplot() method
Seaborn is an amazing visualization library for statistical graphics plotting in Python. It provides beautiful default styles and color palettes to make statistical plots more attractive. It is built on the top of matplotlib library and also closely integrated to the data structures from pandas. seaborn.factorplot() method seaborn.factorplot() meth
5 min read
Python - seaborn.lmplot() method
Seaborn is an amazing visualization library for statistical graphics plotting in Python. It provides beautiful default styles and color palettes to make statistical plots more attractive. It is built on the top of matplotlib library and also closely integrated to the data structures from pandas. seaborn.Implot() method seaborn.lmplot() method is us
7 min read
Seaborn.barplot() method in Python
Seaborn is a Python data visualization library based on Matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics. There is just something extraordinary about a well-designed visualization. The colors stand out, the layers blend nicely together, the contours flow throughout, and the overall package n
4 min read
Python - seaborn.pairplot() method
Prerequisite: Seaborn Programming Basics Seaborn is a Python data visualization library based on matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics. Seaborn helps resolve the two major issues while working with Matplotlib: Default Matplotlib parametersWorking with data frames As Seaborn compli
2 min read
Article Tags :
Practice Tags :