Sectionย 1. Chapterย 8
single
Using 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.
12345678910111213141516import 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()
Task
Swipe to start coding
Visualize the distribution of diamond carats using the diamonds dataset.
- Set the style to
'darkgrid'. - Create a KDE plot using the
displotfunction:- Set
xto'carat'. - Color the curves by
'cut'usinghue. - Split the plot into separate columns based on the
'color'of the diamond usingcol. - Set the type of plot to
'kde'using thekindparameter. - Normalize the data using
multiple='fill'to show relative proportions. - Use the
'viridis'palette. - Use the
dfvariable as data.
- Set
- Display the plot.
Solution
Everything was clear?
Thanks for your feedback!
Sectionย 1. Chapterย 8
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat