HomeLinuxCountplot Seaborn

Countplot Seaborn


In Python, “Seaborn” supplies a high-level interface for creating visually interesting statistical graphics. Seaborn presents a variety of plot sorts, together with scatter plots, line plots, bar plots, depend plots, and so on. The “seaborn.countplot()” methodology of the “seaborn” library can be utilized to visualise the distribution of classes in categorical information. The “countplots” are mainly bar plots that show distinctive values in a categorical variable in a method that enables us to rapidly perceive what number of completely different classes exist inside the dataset.

This Python weblog supplies a whole information on the “seaborn.countplot()” methodology utilizing quite a few examples.

Python “seaborn.countplot()” Methodology

The “seaborn.countplot()” methodology is utilized to find out the variety of information factors per class for a categorical variable and visualize the output as a bar chart. It may be utilized to visualise the distribution of a single categorical variable.

Syntax

seaborn.countplot(x=None, y=None, hue=None, information=None, order=None, hue_order=None, orient=None, coloration=None, palette=None, saturation=0.75, dodge=True, ax=None, **kwargs)

Parameters

In accordance with the above syntax:

  • The “x” and “y” parameters specify the column identify or index of the explicit variable to plot on the “x-axis” and “y-axis”.
  • To encode colours, the “hue” parameter accepts the identify of a column.
  • The “information” parameter specifies the information body that’s utilized to plot graphs.
  • The “order” and “hue_order” parameters are an inventory of strings or numbers specifying the order of the classes on the x-axis and hue-axis.
  • The “orient” parameter signifies the plot orientation (horizontal or vertical).
  • The “coloration”, “palette”, “saturation”, “dodge”, “ax” and “**kwargs” parameters of the “seaborn.countplot()” methodology are non-compulsory.

Return Worth

This methodology returns an axes object representing the plot.

Instance 1: Making use of the “seaborn.countplot()” Methodology

The under instance code makes use of the “seaborn.countplot()” methodology to plot the actual information:

import seaborn

import matplotlib.pyplot as plt

import pandas

information = pandas.DataFrame({‘Worker’: [‘Joseph’, ‘Anna’, ‘Lily’, ‘Max’, ‘Henry’],

‘intercourse’: [‘Male’, ‘Female’, ‘Female’, ‘Male’, ‘Male’]})

seaborn.countplot(x=‘intercourse’, information=information)

plt.present()

Right here on this instance:

  • The “seaborn”, “matplotlib” and “pandas” libraries are imported, respectively.
  • The “seaborn.countplot()” methodology is used to create a bar chart that shows the variety of observations in each categorical bin using bars.
  • Right here, the “x-axis” represents the gender of the workers, and the “y-axis” corresponds to the depend of the workers for every gender.

Output

The visible distribution of the workers primarily based on gender has been displayed within the above output.

Instance 2: Evaluating Two Categorical Variables Based mostly on the Countplot “hue” Parameter

The next instance code compares the 2 categorical variables primarily based on the “hue” parameter of the “countplot()” methodology. The “seaborn” library features a pattern dataset known as “ideas” that may be loaded on this instance:

import seaborn

import matplotlib.pyplot as plt

df = seaborn.load_dataset(‘ideas’)

seaborn.countplot(x =‘intercourse’, hue = “time”, information = df)

plt.present()

Right here on this instance:

  • The “seaborn” and “matplotlib” libraries are imported.
  • The “seaborn.load_dataset()” methodology is used to load the pattern dataset “ideas”.
  • The “seaborn.countplot()” methodology creates a countplot and exhibits the variety of observations in each categorical bin using bars. Right here the “x-axis” is labeled as “intercourse” and the “hue” parameter is labeled as “time”.

Output

The depend plot of a number of categorical variables has been displayed within the above output.

Instance 3: Plotting A number of Horizontal Rely Plots

The under code is used to plot a number of horizontal depend plots:

import seaborn

import matplotlib.pyplot as plt

df = seaborn.load_dataset(‘ideas’)

seaborn.countplot(y =‘intercourse’, hue = “time”, information = df)

plt.present()

Within the above code strains, the “intercourse” worth is assigned to the “y-axis” quite than “x” to regulate the orientation of the depend plot created utilizing the “seaborn.countplot()” methodology.

Output

Right here, it may be visualized that the orientation of the “countplot” has been modified accordingly.

Conclusion

The “countplot()” methodology of the “seaborn” library is used to depend the variety of observations per class and visualize the outcomes. The “seaborn.countplot()” methodology supplies a number of parameters comparable to “orient”, “coloration”, “palette”, “saturation” and so on. which can be utilized to execute sure operations on the plot. This Python information introduced a whole information on the “seaborn.countplot()” methodology utilizing quite a few examples.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments