Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Using the Distribution Interface | Section
Statistical Visualization with Seaborn
Sectionย 1. Chapterย 8
single

single

bookUsing the Distribution Interface

Swipe to show menu

The displot (distribution plot) is the "parent" function for all the plots we have learned so far. It provides a single interface to create histograms, KDE plots, and ECDFs.

Why Use Displot?

While histplot and kdeplot are great for single plots, displot has a superpower: faceting.

Using the col (column) or row parameter, displot can automatically split your dataset into multiple side-by-side subplots based on a category.

Key Parameters

  • kind: determines the type of plot;
    • 'hist' (default);
    • 'kde';
    • 'ecdf'.
  • col / row: splits the data into separate subplots arranged in columns or rows.

Example

Here is how you can instantly create 3 separate histograms for different species using just one line of code.

12345678910111213141516
import seaborn as sns import matplotlib.pyplot as plt # Load dataset df = sns.load_dataset('penguins') # Create a grid of plots sns.displot( data=df, x='bill_length_mm', col='species', # Creates 3 subplots (one per species) kind='hist', # Draw histograms element='step' ) plt.show()
copy
Task

Swipe to start coding

Visualize the distribution of diamond carats using the diamonds dataset.

  1. Set the style to 'darkgrid'.
  2. Create a KDE plot using the displot function:
    • Set x to 'carat'.
    • Color the curves by 'cut' using hue.
    • Split the plot into separate columns based on the 'color' of the diamond using col.
    • Set the type of plot to 'kde' using the kind parameter.
    • Normalize the data using multiple='fill' to show relative proportions.
    • Use the 'viridis' palette.
    • Use the df variable as data.
  3. Display the plot.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Sectionย 1. Chapterย 8
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

some-alt