| by Arround The Web | No comments

Seaborn Catplot

Categorical plots display the relationship between numerical and multiple categorical variables. Different methods are utilized to create/construct a categorical plot in Python. Besides the dedicated methods for each plot type, such as “seaborn.stripplot()” or “seaborn.barplot()“, Python provides the “seaborn.catplot()” method for creating categorical plots.

This Python post presents a thorough guide on the “seaborn.catplot()” method with the help of the below-covered content:

What is the “seaborn.catplot()” Method in Python?

In Python, the “seaborn.catplot()” method is used for making categorical plots. It provides an easy way to create the plots at the overall figure level rather than focusing on individual parts.

Syntax

The syntax of the “seaborn.catplot()” method is shown below:

seaborn.catplot(data=None, *, x=None, y=None, hue=None, row=None, col=None, col_wrap=None, estimator='mean', errorbar=('ci', 95), n_boot=1000, units=None, seed=None, order=None, hue_order=None, row_order=None, col_order=None, height=5, aspect=1, kind='strip', native_scale=False, formatter=None, orient=None, color=None, palette=None, hue_norm=None, legend='auto', legend_out=True, sharex=True, sharey=True, margin_titles=False, facet_kws=None, ci='deprecated', **kwargs)

Parameter Value

In the above syntax:

  • The “data” parameter specifies the data to be plotted. This should be a Pandas DataFrame or a NumPy array.
  • The “x” and “y” parameters indicate the column’s name in the data containing the categorical variable to be displayed on the x-axis and y-axis.
  • The “hue” parameter indicates the column’s name in the data containing the categorical variable to be used for coloring the points.
  • The “row” and “col” parameters indicate the column’s name in the data that should be used to create separate subplots for each variable level.
  • The “kind” parameter specifies the plot type to be created. The possible values for this parameter include strip, swarm, box, violin, and boxen.
  • Please refer to this official documentation for all the other parameters.

Return Value

The retrieved value of the “seaborn.catplot()” method is a “FacetGrid” object, which can be used to further customize the appearance of the plot.

Example 1: Applying the “seaborn.catplot()” Method to Create a Categorical Plot

The “seaborn.catplot()” method can be used to create a variety of categorical plots, including strip plots, swarm plots, box plots, violin plots, and boxen plots. The type of plot that is generated is determined by the “kind” parameter/attribute.

The following code will create a “strip” plot that shows the distribution of the “total_bill” variable for each level of the “day” and “smoker” variables:

import seaborn

import matplotlib.pyplot as plt

data = seaborn.load_dataset("tips")

seaborn.catplot(data=data, x="day", y="total_bill", hue="smoker", kind="strip")

plt.show()

In the above code:

  • The “seaborn” and the “matplotlib.pyplot” modules are imported.
  • The built-in dataset from Seaborn named “tips” is loaded. This dataset contains information about the tips given by customers at a restaurant.
  • The “seaborn.catplot()” method creates a categorical plot. This plot displays the distribution of the total bill amount for each day of the week, grouped by whether the customer is a smoker or not.
  • The kind of plot is a “strip“, which shows each data point by a dot.
  • Lastly, the “plt.show()” function displays the plot.

Output

The strip plot has been created.

Example 2: Applying the “seaborn.catplot()” Method to Create a Categorical Plot With a Different “kind” Parameter Value

Let’s explore the below code:

import seaborn

import matplotlib.pyplot as plt

data = seaborn.load_dataset("tips")

seaborn.catplot(data=data, x="day", y="total_bill", hue="smoker", kind="box")

plt.show()

In this block of code, the “kind” parameter is set to “box“, so a “box plot” will be created. This box plot will show the distribution of the “total_bill” variable for each level of the “day” and “smoker” values. The boxes will be colored differently depending on the value of “smoker”.

Output

As visualized, the box plot has been created in this case appropriately.

Conclusion

In Python, the “seaborn.catplot()” method of the “seaborn” module is used to create a variety of categorical plots, including strip plots, swarm plots, etc. This method takes several parameters such as data, x, y, hue, etc. The “kind” parameter indicates the “type” of the plot to be created. This article delivered a comprehensive tutorial on Python’s “seaborn.catplot()” method using numerous examples.

Share Button

Source: linuxhint.com

Leave a Reply