📂

catplot

Codes

sns.catplot()

Notes

Box plot to display data distribution and outliers across categories.

Status
Done

sns.catplot() is a flexible function in Seaborn that allows you to visualize categorical data in multiple ways. It combines various plot types under a single interface, making it easy to compare and explore data distributions.

General Syntax

sns.catplot(
    data,
    x=None,
    y=None,
    hue=None,
    col=None,
    row=None,
    kind='strip',
    height=5,
    aspect=1,
    order=None,
    hue_order=None,
    col_order=None,
    row_order=None,
    palette=None,
    margin_titles=False,
    legend_out=True,
    sharex=True,
    sharey=True,
    dodge=True
)

All Supported Plot Types with kind

Sn
kind
Description
1
strip
Scatterplot for categorical data, with optional jitter.
2
swarm
Non-overlapping scatterplot.
3
box
Box plot showing medians, quartiles, and outliers.
4
violin
Combines box plot with KDE for distribution visualization.
5
boxen
Enhanced box plot optimized for large datasets.
6
bar
Bar plot showing the mean and confidence interval.
7
point
Point plot showing means with confidence intervals, connected by lines.
8
count
Bar plot of counts for each category (no y variable required).

3. Dataset Setup

We will use the tips dataset for all examples.

import seaborn as sns
import matplotlib.pyplot as plt

# Load the dataset
data = sns.load_dataset('tips')
data.head()

The tips dataset includes:

  • sex: Gender of the customer.
  • smoker: Smoking status.
  • day: Day of the week.
  • time: Meal type (Lunch/Dinner).
  • size: Party size.
  • total_bill and tip: Bill and tip amounts.
total_bill
tip
sex
smoker
day
time
size
0
16.99
1.01
Female
No
Sun
Dinner
2
1
10.34
1.66
Male
No
Sun
Dinner
3
2
21.01
3.50
Male
No
Sun
Dinner
3
3
23.68
3.31
Male
No
Sun
Dinner
2
4
24.59
3.61
Female
No
Sun
Dinner
4

1. kind='strip'


sns.catplot(data=data, 
            x='day', 
            y='total_bill', 
            kind='strip', 
            jitter=True)
            
plt.title("Strip Plot of Total Bill by Day")
plt.show()
image

2. kind='swarm'

sns.catplot(data=data, 
            x='day', 
            y='total_bill', 
            kind='swarm', 
            hue='sex')
            
plt.title("Swarm Plot of Total Bill by Day with Hue")
plt.show()
image

3. kind='box'

sns.catplot(data=data, 
            x='day', 
            y='total_bill', 
            kind='box', 
            hue='smoker')
            
plt.title("Box Plot of Total Bill by Day")
plt.show()
image

if we want to remove the outlier from the chat, we use the fliersize=0 as shown below

sns.catplot(data=data, 
            x='day', 
            y='total_bill', 
            kind='box', 
            hue='smoker',
              fliersize=0)
            
plt.title("Box Plot of Total Bill by Day")
plt.show()
image

4. kind='violin'

sns.catplot(data=data, 
            x='day', 
            y='total_bill', 
            kind='violin', 
            hue='smoker', 
            split=True)
            
plt.title("Violin Plot of Total Bill by Day")
plt.show()
image

5. kind='boxen'

sns.catplot(data=data, 
            x='day', 
            y='total_bill', 
            kind='boxen')
            
plt.title("Boxen Plot of Total Bill by Day")
plt.show(
image

6. kind='bar'

sns.catplot(data=data, 
            x='day', 
            y='total_bill', 
            kind='bar', 
            hue='time', 
            errorbar='sd')
plt.title("Bar Plot of Total Bill by Day")
plt.show()
image

7. kind='point'

sns.catplot(data=data, 
            x='day', 
            y='total_bill', 
            kind='point', 
            hue='time')
plt.title("Point Plot of Total Bill by Day")
plt.show()
image

8. kind='count'

sns.catplot(data=data, 
            x='day', 
            kind='count', 
            hue='sex')
plt.title("Count Plot of Days with Hue")
plt.show()
image

e. order and hue_order

Example: Custom order for day and smoker hue.

sns.catplot(
    data=data,
    x='day',
    y='total_bill',
    hue='smoker',
    kind='bar',
    order=['Sat', 'Sun', 'Thur', 'Fri'],
    hue_order=['Yes', 'No']
)
plt.show()
image

f. palette

Example: Apply a custom color palette.

sns.catplot(data=data, 
            x='day', 
            y='total_bill', 
            kind='bar', 
            hue='smoker', 
            palette='coolwarm')
plt.show()
image

g. height and aspect

Example: Adjust the size of the plot.

sns.catplot(data=data, 
            x='day', 
            y='total_bill', 
            kind='bar', 
            height=6, 
            aspect=1.5)
plt.show()
image

h. margin_titles

Example: Place facet titles in margins.

sns.catplot(data=data, 
            x='day', 
            y='total_bill', 
            col='sex', 
            kind='bar', 
            margin_titles=True)
plt.show()
image

i. legend_out

Example: Keep the legend inside the plot.

sns.catplot(data=data, 
            x='day', 
            y='total_bill', 
            kind='violin', 
            hue='smoker', 
            legend_out=False)
plt.show()
image

j. sharex and sharey

Example: Allow different y-axis scales for each subplot.

sns.catplot(data=data, 
            x='day', 
            y='total_bill', 
            col='time', 
            kind='bar', 
            sharey=False)
plt.show()
image

k. dodge

Example: Separate hue categories for visibility.

sns.catplot(data=data, 
            x='day', 
            y='total_bill', 
            hue='sex', 
            kind='swarm', 
            dodge=True)
plt.show()
image

5. Final Practice Example

Combine multiple parameters:

sns.catplot(
    data=data,
    x='day',
    y='total_bill',
    hue='smoker',
    col='sex',
    row='time',
    kind='bar',
    height=6,
    aspect=1.2,
    palette='muted',
    margin_titles=True,
    sharey=False
)
plt.show()
image