Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Enhancing Plots with Rugs | Section
Statistical Visualization with Seaborn
Section 1. Chapter 6
single

single

bookEnhancing 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 like 0.05 or 0.1 is usually best to keep it subtle;
  • hue: groups data by color, allowing you to match the categories in your main plot;
  • x or y: determines which axis the data lies on.
12345678910111213
import 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()
copy
Task

Swipe to start coding

Now, let's apply this to create a styled visualization for the tips dataset.

  1. 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'.
  2. Create a KDE plot for 'total_bill', grouped by 'sex':
    • Use the 'magma' palette;
    • Stack the layers (multiple='layer');
    • Fill the curves (fill=True).
  3. Add a Rugplot to show individual data points:
    • Use the same x-axis ('total_bill') and grouping ('sex');
    • Set the height to 0.05;
    • Use the same 'magma' palette.
  4. 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 6
single

single

Ask AI

expand

Ask AI

ChatGPT

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

some-alt