Section 1. Chapter 6
single
Enhancing Plots with Rugs
Swipe to show menu
The rugplot is a plot intended to complement other plots by showing the location of individual observations in an unobtrusive way.
While kdeplot shows a smooth probability curve, it can sometimes hide the fact that there are very few data points in a certain area. Adding a rugplot draws small "ticks" (like fringes on a rug) for every single data point along the x or y axis.
Why Combine Them?
- KDE: shows the abstract shape (trend).
- Rugplot: shows the actual data density (reality).
Key Parameters
To make the rugplot effective and compatible with other plots, you should know these parameters:
height: controls the length of the ticks relative to the plot area. A value like0.05or0.1is usually best to keep it subtle;hue: groups data by color, allowing you to match the categories in your main plot;xory: determines which axis the data lies on.
12345678910111213import seaborn as sns import matplotlib.pyplot as plt # Load the dataset df = sns.load_dataset('tips') # 1. Main plot (Abstract shape) sns.kdeplot(data=df, x='total_bill', fill=True, alpha=0.3) # 2. Rug plot (Real data points) sns.rugplot(data=df, x='total_bill', height=0.1, color='black') plt.show()
Task
Swipe to start coding
Now, let's apply this to create a styled visualization for the tips dataset.
- Set the style to
'darkgrid'. Pass a dictionary as the second argument to configure the grid: disable'axes.grid'(False) and set'axes.facecolor'to'aliceblue'. - Create a KDE plot for
'total_bill', grouped by'sex':- Use the
'magma'palette; - Stack the layers (
multiple='layer'); - Fill the curves (
fill=True).
- Use the
- Add a Rugplot to show individual data points:
- Use the same x-axis (
'total_bill') and grouping ('sex'); - Set the
heightto0.05; - Use the same
'magma'palette.
- Use the same x-axis (
- Display the plot.
Solution
Everything was clear?
Thanks for your feedback!
Section 1. Chapter 6
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat