Skip to content

Commit 57edbc0

Browse files
feat(altair): implement ridgeline-basic (#544)
## Summary Implements `ridgeline-basic` for **altair**. **Parent Issue:** #539 **Base Branch:** `plot/ridgeline-basic` ## Files - `plots/ridgeline-basic/implementations/altair.py` ## Preview Preview will be uploaded to GCS staging after this workflow completes. Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent e493fe6 commit 57edbc0

File tree

1 file changed

+86
-0
lines changed
  • plots/ridgeline-basic/implementations

1 file changed

+86
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
"""
2+
ridgeline-basic: Ridgeline Plot
3+
Library: altair
4+
"""
5+
6+
import altair as alt
7+
import numpy as np
8+
import pandas as pd
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 temperature data with seasonal patterns
29+
data = []
30+
for i, month in enumerate(months):
31+
# Seasonal temperature pattern (Northern Hemisphere)
32+
base_temp = 5 + 20 * np.sin(np.pi * (i - 3) / 6) # Peak in July
33+
temps = np.random.normal(base_temp, 5, 150)
34+
for temp in temps:
35+
data.append({"month": month, "temperature": temp})
36+
37+
df = pd.DataFrame(data)
38+
39+
# Create a step value for vertical positioning
40+
month_order = {month: i for i, month in enumerate(months)}
41+
df["month_num"] = df["month"].map(month_order)
42+
43+
# Define colors - seasonal gradient (blue=cold, orange=warm)
44+
# Colors match temperature patterns: cold winters (blue) to warm summers (orange)
45+
colors = [
46+
"#306998", # January - Python Blue (cold)
47+
"#3b7ba8", # February
48+
"#59a0c4", # March
49+
"#8cc4d4", # April
50+
"#b4d4d8", # May
51+
"#f0b070", # June
52+
"#f97316", # July - Orange (peak summer)
53+
"#f99548", # August
54+
"#e0c9a8", # September
55+
"#8cc4d4", # October
56+
"#59a0c4", # November
57+
"#306998", # December - Python Blue (cold)
58+
]
59+
60+
# Create ridgeline using faceted area charts
61+
# Each row is a separate density chart, vertically offset
62+
chart = (
63+
alt.Chart(df)
64+
.transform_density(density="temperature", as_=["temperature", "density"], groupby=["month"], extent=[-15, 45])
65+
.mark_area(fillOpacity=0.7, stroke="white", strokeWidth=1)
66+
.encode(
67+
x=alt.X("temperature:Q", title="Temperature (°C)", scale=alt.Scale(domain=[-15, 45])),
68+
y=alt.Y("density:Q", title=None, scale=alt.Scale(range=[80, 0]), axis=None),
69+
fill=alt.Fill("month:N", scale=alt.Scale(domain=months, range=colors), legend=None),
70+
row=alt.Row(
71+
"month:N",
72+
title=None,
73+
header=alt.Header(labelAngle=0, labelAlign="right", labelFontSize=14, labelPadding=10),
74+
sort=months,
75+
spacing=-30, # Negative spacing creates overlap
76+
),
77+
)
78+
.properties(width=1400, height=60, title="Monthly Temperature Distributions")
79+
.configure_view(stroke=None)
80+
.configure_facet(spacing=0)
81+
.configure_title(fontSize=24, anchor="middle")
82+
)
83+
84+
# Save outputs
85+
chart.save("plot.png", scale_factor=3.0)
86+
chart.save("plot.html")

0 commit comments

Comments
 (0)