|
| 1 | +""" |
| 2 | +ridgeline-basic: Ridgeline Plot |
| 3 | +Library: seaborn |
| 4 | +""" |
| 5 | + |
| 6 | +import numpy as np |
| 7 | +import pandas as pd |
| 8 | +import seaborn as sns |
| 9 | + |
| 10 | + |
| 11 | +# Data - Monthly temperature distributions |
| 12 | +np.random.seed(42) |
| 13 | +months = [ |
| 14 | + "January", |
| 15 | + "February", |
| 16 | + "March", |
| 17 | + "April", |
| 18 | + "May", |
| 19 | + "June", |
| 20 | + "July", |
| 21 | + "August", |
| 22 | + "September", |
| 23 | + "October", |
| 24 | + "November", |
| 25 | + "December", |
| 26 | +] |
| 27 | + |
| 28 | +# Generate realistic temperature data with seasonal pattern |
| 29 | +data = [] |
| 30 | +base_temps = [2, 4, 8, 12, 17, 21, 24, 23, 19, 13, 7, 3] # Typical temperate climate |
| 31 | +for i, month in enumerate(months): |
| 32 | + temps = np.random.normal(base_temps[i], 3, 150) |
| 33 | + for t in temps: |
| 34 | + data.append({"month": month, "temperature": t}) |
| 35 | + |
| 36 | +df = pd.DataFrame(data) |
| 37 | +df["month"] = pd.Categorical(df["month"], categories=months, ordered=True) |
| 38 | + |
| 39 | +# Color palette - gradient from cool to warm |
| 40 | +colors = sns.color_palette("coolwarm", n_colors=12) |
| 41 | + |
| 42 | +# Create ridgeline plot using FacetGrid |
| 43 | +sns.set_style("white") |
| 44 | +g = sns.FacetGrid( |
| 45 | + df, |
| 46 | + row="month", |
| 47 | + hue="month", |
| 48 | + aspect=12, |
| 49 | + height=0.7, |
| 50 | + palette=colors, |
| 51 | + row_order=months[::-1], # Reverse order so January at bottom |
| 52 | + hue_order=months[::-1], |
| 53 | +) |
| 54 | + |
| 55 | +# Draw density plots |
| 56 | +g.map(sns.kdeplot, "temperature", bw_adjust=0.8, clip_on=False, fill=True, alpha=0.7, linewidth=1.5) |
| 57 | + |
| 58 | +# Add outline |
| 59 | +g.map(sns.kdeplot, "temperature", bw_adjust=0.8, clip_on=False, color="black", lw=1) |
| 60 | + |
| 61 | +# Add horizontal line at y=0 |
| 62 | +g.refline(y=0, linewidth=1, linestyle="-", color="black", clip_on=False) |
| 63 | + |
| 64 | +# Styling - overlap the rows |
| 65 | +g.figure.subplots_adjust(hspace=-0.3) |
| 66 | + |
| 67 | +# Remove axes details |
| 68 | +g.set_titles("") |
| 69 | +g.set(yticks=[], ylabel="") |
| 70 | +g.despine(bottom=True, left=True) |
| 71 | + |
| 72 | +# Add month labels on the left |
| 73 | +for i, ax in enumerate(g.axes.flat): |
| 74 | + ax.text( |
| 75 | + -0.02, |
| 76 | + 0.1, |
| 77 | + months[::-1][i], |
| 78 | + fontweight="bold", |
| 79 | + fontsize=14, |
| 80 | + color=colors[::-1][i], |
| 81 | + ha="right", |
| 82 | + va="center", |
| 83 | + transform=ax.transAxes, |
| 84 | + ) |
| 85 | + |
| 86 | +# Set x-axis label on bottom plot only |
| 87 | +g.axes[-1, 0].set_xlabel("Temperature (°C)", fontsize=16) |
| 88 | +g.axes[-1, 0].tick_params(axis="x", labelsize=12) |
| 89 | + |
| 90 | +# Title |
| 91 | +g.figure.suptitle("Monthly Temperature Distribution", fontsize=20, fontweight="bold", y=0.98) |
| 92 | + |
| 93 | +# Save |
| 94 | +g.figure.set_size_inches(16, 9) |
| 95 | +g.savefig("plot.png", dpi=300, bbox_inches="tight") |
0 commit comments