| by Arround The Web | No comments

Countplot Seaborn

In Python, “Seaborn” provides a high-level interface for creating visually appealing statistical graphics. Seaborn offers a wide range of plot types, including scatter plots, line plots, bar plots, count plots, etc. The “seaborn.countplot()” method of the “seaborn” library can be used to visualize the distribution of categories in categorical data. The “countplots” are basically bar plots that display unique values in a categorical variable in a way that allows us to quickly understand how many different categories exist within the dataset.

This Python blog provides a complete guide on the “seaborn.countplot()” method using numerous examples.

Python “seaborn.countplot()” Method

The “seaborn.countplot()” method is utilized to determine the number of data points per category for a categorical variable and visualize the output as a bar chart. It can be utilized to visualize the distribution of a single categorical variable.

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

According to the above syntax:

  • The “x” and “y” parameters specify the column name or index of the categorical variable to plot on the “x-axis” and “y-axis”.
  • To encode colors, the “hue” parameter accepts the name of a column.
  • The “data” parameter specifies the data frame that is utilized to plot graphs.
  • The “order” and “hue_order” parameters are a list of strings or numbers specifying the order of the categories on the x-axis and hue-axis.
  • The “orient” parameter indicates the plot orientation (horizontal or vertical).
  • The “color”, “palette”, “saturation”, “dodge”, “ax” and “**kwargs” parameters of the “seaborn.countplot()” method are optional.

Return Value

This method returns an axes object representing the plot.

Example 1: Applying the “seaborn.countplot()” Method

The below example code utilizes the “seaborn.countplot()” method to plot the particular data:

import seaborn

import matplotlib.pyplot as plt

import pandas

data = pandas.DataFrame({'Employee': ['Joseph', 'Anna', 'Lily', 'Max', 'Henry'],

'sex': ['Male', 'Female', 'Female', 'Male', 'Male']})

seaborn.countplot(x='sex', data=data)

plt.show()

Here in this example:

  • The “seaborn”, “matplotlib” and “pandas” libraries are imported, respectively.
  • The “seaborn.countplot()” method is used to create a bar chart that displays the number of observations in every categorical bin utilizing bars.
  • Here, the “x-axis” represents the gender of the employees, and the “y-axis” corresponds to the count of the employees for each gender.

Output

The visual distribution of the employees based on gender has been displayed in the above output.

Example 2: Comparing Two Categorical Variables Based on the Countplot “hue” Parameter

The following example code compares the two categorical variables based on the “hue” parameter of the “countplot()” method. The “seaborn” library includes a sample dataset called “tips” that can be loaded in this example:

import seaborn

import matplotlib.pyplot as plt

df = seaborn.load_dataset('tips')

seaborn.countplot(x ='sex', hue = "time", data = df)

plt.show()

Here in this example:

  • The “seaborn” and “matplotlib” libraries are imported.
  • The “seaborn.load_dataset()” method is used to load the sample dataset “tips”.
  • The “seaborn.countplot()” method creates a countplot and shows the number of observations in every categorical bin utilizing bars. Here the “x-axis” is labeled as “sex” and the “hue” parameter is labeled as “time”.

Output

The count plot of multiple categorical variables has been displayed in the above output.

Example 3: Plotting Multiple Horizontal Count Plots

The below code is used to plot multiple horizontal count plots:

import seaborn

import matplotlib.pyplot as plt

df = seaborn.load_dataset('tips')

seaborn.countplot(y ='sex', hue = "time", data = df)

plt.show()

In the above code lines, the “sex” value is assigned to the “y-axis” rather than “x” to adjust the orientation of the count plot created using the “seaborn.countplot()” method.

Output

Here, it can be visualized that the orientation of the “countplot” has been changed accordingly.

Conclusion

The “countplot()” method of the “seaborn” library is used to count the number of observations per category and visualize the results. The “seaborn.countplot()” method provides several parameters such as “orient”, “color”, “palette”, “saturation” etc. that are utilized to execute certain operations on the plot. This Python guide presented a complete guide on the “seaborn.countplot()” method using numerous examples.

Share Button

Source: linuxhint.com

Leave a Reply