How to make a rug plot in Matplotlib?

A rug plot is a one-dimensional visualization that displays data points as marks along an axis, making it easy to see the distribution and density of values. In Matplotlib, rug plots are often combined with density curves to provide a comprehensive view of data distribution.

Basic Rug Plot

Let's start with a simple rug plot using sample data points ?

import numpy as np
import matplotlib.pyplot as plt

# Sample data points
data = np.array([-6, -4, 2, 1, 4], dtype=np.float)

# Create figure and axis
fig, ax = plt.subplots(figsize=(8, 4))

# Create rug plot - plot data points at y=0
ax.plot(data, np.zeros(data.shape), 'b+', markersize=20, markeredgewidth=2)

# Set labels and title
ax.set_xlabel('Value')
ax.set_ylabel('Density')
ax.set_title('Simple Rug Plot')
ax.grid(True, alpha=0.3)

plt.show()
[A rug plot showing blue plus signs at positions -6, -4, 1, 2, and 4 along the x-axis at y=0]

Rug Plot with Kernel Density Estimation

Combining rug plots with kernel density estimation (KDE) provides better insight into data distribution ?

import numpy as np
from scipy import stats
import matplotlib.pyplot as plt

# Sample data points
data = np.array([-6, -4, 2, 1, 4], dtype=np.float)

# Create KDE estimates using different bandwidth methods
kde_scott = stats.gaussian_kde(data)  # Scott's rule (default)
kde_silverman = stats.gaussian_kde(data, bw_method='silverman')  # Silverman's rule

# Create evaluation points for smooth curves
x_eval = np.linspace(-8, 6, 100)

# Create the plot
fig, ax = plt.subplots(figsize=(10, 6))

# Add rug plot at the bottom
ax.plot(data, np.zeros(data.shape), 'b+', markersize=15, markeredgewidth=2, label='Data points')

# Plot KDE curves
ax.plot(x_eval, kde_scott(x_eval), 'k-', linewidth=2, label="Scott's Rule")
ax.plot(x_eval, kde_silverman(x_eval), 'r-', linewidth=2, label="Silverman's Rule")

# Customize the plot
ax.set_xlabel('Value')
ax.set_ylabel('Density')
ax.set_title('Rug Plot with Kernel Density Estimation')
ax.legend(loc='upper right')
ax.grid(True, alpha=0.3)

plt.tight_layout()
plt.show()
[A plot showing data points as blue plus signs at the bottom, with two smooth density curves above - one black line (Scott's rule) and one red line (Silverman's rule)]

Customizing Rug Plot Markers

You can customize the appearance of rug plot markers using different styles and colors ?

import numpy as np
import matplotlib.pyplot as plt

# Generate sample data
np.random.seed(42)
data1 = np.random.normal(0, 1, 50)
data2 = np.random.normal(2, 0.5, 30)

fig, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize=(10, 8))

# Different marker styles
ax1.plot(data1, np.zeros(len(data1)), '|', markersize=10, color='blue', alpha=0.7)
ax1.set_title('Rug Plot with Vertical Lines')
ax1.set_ylabel('Density')

ax2.plot(data1, np.zeros(len(data1)), 'o', markersize=3, color='red', alpha=0.6)
ax2.set_title('Rug Plot with Circles')
ax2.set_ylabel('Density')

# Combined datasets
ax3.plot(data1, np.zeros(len(data1)), '|', markersize=8, color='blue', alpha=0.7, label='Dataset 1')
ax3.plot(data2, np.full(len(data2), 0.02), '|', markersize=8, color='red', alpha=0.7, label='Dataset 2')
ax3.set_title('Comparing Two Datasets')
ax3.set_xlabel('Value')
ax3.set_ylabel('Density')
ax3.legend()

plt.tight_layout()
plt.show()
[Three subplots showing different rug plot styles: vertical lines, circles, and a comparison of two datasets with offset positions]

Key Parameters

Parameter Description Example Values
marker Shape of the markers '|', '+', 'o', 's'
markersize Size of the markers 5, 10, 15, 20
alpha Transparency level 0.3, 0.5, 0.7, 1.0
color Color of the markers 'blue', 'red', '#FF5733'

Conclusion

Rug plots are excellent for visualizing data distribution, especially when combined with density curves. Use vertical lines ('|') for traditional rug plots, and experiment with marker styles and colors to highlight different datasets or categories.

Updated on: 2026-03-26T02:29:49+05:30

915 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements