Python seaborn.catplot() method

To plot categorical plots, use the Seaborn. catplot () function. This function provides access to a variety of axes-level functions that show the relationship between numerical data and one or more category variables using one of several available visual representations. The type parameter selects the underlying axes-level function.

Below are the numerous category plots that may be created, along with the values that the type parameter accepts.

  • The three types of categorical scatter plots are the swarm, strip, and cat plots.
  • Plots of categorical distribution include the box, violin, and boxen plots.
  • Plots for categorical estimates include bar, point, and count plots.

The following is the syntax of seaborn.catplot() method:

Syntax

Loading the Seaborn Library

The code below may be used to import or load the Seaborn library.

Loading the dataset

This post will use the Tips dataset, which is pre-installed in the Seaborn library. To load the dataset, run the following command.

The command listed below can be used to see the first five rows of the dataset. This helps us comprehend the variables that can be plotted on a graph.

The result of the code mentioned above is shown below.

We are going to visualise the data now that it has been loaded.

Example 1:

We will learn how to create a basic plot () in this example by providing the x, y, and hue arguments to the function.

You may use the following line of code to do this.

Explanation:

The code loads the "tips" dataset, which contains details on restaurant bills, gratuities, and other variables like the time of day (lunch or supper) after importing the Seaborn and Matplotlib libraries. For a glance at the data, see the tips.head() method shows the top few rows of this dataset. Next, a scatter plot with x="total_bill," y="tip," and hue="time" is made using the sns. catplot function to distinguish points according to whether the meal was eaten for lunch or supper. Lastly, the plot is rendered by calling plt. show(). This scatter plot illustrates any variations between lunch and supper service hours and aids in visualising the link between total bill amounts and tips.

Output:

Python seaborn.catplot() method

Example 2:

Using the height and aspect parameters, we will see how to change the plot's size in this example. We'll set settings for these parameters and track how the plot's size changes.

Explanation:

The code loads the "tips" dataset, which includes details on restaurant bills, gratuities, and other variables like the time of day (lunch or supper) and the sex of the patrons after importing the Seaborn and Matplotlib libraries. For a quick glance at the data, the tips.head() function shows the top few rows of the dataset. A categorical plot is made using the sns. catplot function, using the parameters x="sex" and y="tip" to compare the tips left by male and female customers. The data points are distinguished according to whether the food was eaten at lunch or supper using the hue="time" parameter. The plot's size and aspect ratio are adjusted with the parameters height=5 and aspect=.8. Lastly, the plot is rendered by calling plt. show().

Output:

This will result in an output graph that looks like the one below.

Python seaborn.catplot() method

Example 3:

Since categorical plots are plotted using catplot(), we will comprehend how to use the row and col parameters. The row and col parameters are used to plot categorical variables from the dataframe on the facet.

The tips dataset, which we are utilising, has four categorical variables: smoker, day, time, and sex. In the graphic that follows, we are using each of these four variables.

Explanation:

The "tips" dataset, which includes details on restaurant bills, tips, and other factors like the day of the week, the time of day (lunch or supper), if the client was a smoker, and the customer's sex, is loaded after the code imports the Seaborn and Matplotlib libraries. For a glance at the data, see the tips.head() function shows the top few rows of the dataset. To generate a complicated category plot, use the sns. catplot function and provide x="day" and y="tip" to display the variation in tips on different days of the week. The data points are distinguished according to whether the food was eaten at lunch or supper using the hue="time" parameter. Plots are arranged into a grid using the col="smoker" and row="sex" options, which make subplots for any combination of sex and smoker status. Lastly, the plot is rendered by calling plt. show(). This detailed map makes it easier to see how tip amounts fluctuate depending on the day, time of day, and client attributes like sex and smoking status.

The catplot() function may be used with the code above to plot each of the tips dataset's four category variables.

Output:

Python seaborn.catplot() method

Example 4:

The seaborn.catplot() function has many arguments. Kind is an extremely helpful metric. The type of plot drawn depends on the value supplied to this option. Various values, including "strip," "swarm," "box," "violin," "boxen," "point," "bar," or "count," can be assigned to it.

The following code demonstrates how this argument functions.

Explanation:

After loading the "tips" dataset-which includes data on restaurant bills, tips, and other variables including the day of the week, the time of day (lunch or supper), and the sex of the patrons-the code imports the Seaborn and Matplotlib libraries. For a glance at the data, see the tips.head() function shows the top few rows of the dataset. A categorical plot is made using the sns. catplot function, using the parameters x="day" and y="tip" to display the variation in tips on different days of the week. While the col="time" option provides distinct subplots for lunch and evening, the hue="sex" parameter separates the data points based on the sex of the clients. A swarm plot, which organises the points to prevent overlap and offer a clear perspective of the distribution, is specified by the kind="swarm" argument. Lastly, the plot is rendered by calling plt. show(). This plot facilitates the visualisation of the differences in tip amounts across various days, the differentiation of male and female patrons, and the division of the data according to meal times (lunch and dinner).

Output:

Python seaborn.catplot() method