Skip to content

Commit 5164327

Browse files
feat(plotnine): implement ridgeline-basic
1 parent 50821a6 commit 5164327

1 file changed

Lines changed: 84 additions & 0 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
"""
2+
ridgeline-basic: Ridgeline Plot
3+
Library: plotnine
4+
"""
5+
6+
import numpy as np
7+
import pandas as pd
8+
from plotnine import (
9+
aes,
10+
element_blank,
11+
element_line,
12+
element_text,
13+
facet_wrap,
14+
geom_density,
15+
ggplot,
16+
labs,
17+
scale_fill_manual,
18+
scale_y_continuous,
19+
theme,
20+
theme_minimal,
21+
)
22+
23+
24+
# Data - Monthly temperature readings
25+
np.random.seed(42)
26+
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
27+
n_per_month = 100
28+
29+
# Generate temperature data with seasonal pattern
30+
data_list = []
31+
base_temps = [2, 4, 8, 12, 17, 21, 24, 23, 19, 13, 7, 3] # Typical seasonal pattern
32+
33+
for i, month in enumerate(months):
34+
temps = np.random.normal(base_temps[i], 3, n_per_month)
35+
data_list.append(pd.DataFrame({"month": month, "temperature": temps}))
36+
37+
data = pd.concat(data_list, ignore_index=True)
38+
39+
# Convert month to ordered categorical (reversed for ridgeline stacking - Dec at top)
40+
data["month"] = pd.Categorical(data["month"], categories=months[::-1], ordered=True)
41+
42+
# Create gradient colors from cool to warm (matching seasonal pattern)
43+
colors = {
44+
"Jan": "#306998",
45+
"Feb": "#3B7AAD",
46+
"Mar": "#4D8BC2",
47+
"Apr": "#5F9CD7",
48+
"May": "#71ADEC",
49+
"Jun": "#FFD43B",
50+
"Jul": "#F97316",
51+
"Aug": "#DC2626",
52+
"Sep": "#F97316",
53+
"Oct": "#FFD43B",
54+
"Nov": "#71ADEC",
55+
"Dec": "#306998",
56+
}
57+
58+
# Create ridgeline plot using facet_wrap for vertical stacking
59+
plot = (
60+
ggplot(data, aes(x="temperature", fill="month"))
61+
+ geom_density(alpha=0.7, color="white", size=0.5)
62+
+ facet_wrap("~month", ncol=1, scales="free_y")
63+
+ scale_fill_manual(values=colors)
64+
+ labs(x="Temperature (\u00b0C)", y="", title="Monthly Temperature Distribution")
65+
+ theme_minimal()
66+
+ theme(
67+
figure_size=(16, 9),
68+
plot_title=element_text(size=20),
69+
axis_title_x=element_text(size=20),
70+
axis_text_x=element_text(size=16),
71+
axis_text_y=element_blank(),
72+
axis_ticks_major_y=element_blank(),
73+
strip_text=element_text(size=14),
74+
strip_background=element_blank(),
75+
legend_position="none",
76+
panel_spacing_y=-0.3,
77+
panel_grid=element_blank(),
78+
axis_line_x=element_line(color="#333333", size=0.5),
79+
)
80+
+ scale_y_continuous(expand=(0, 0))
81+
)
82+
83+
# Save
84+
plot.save("plot.png", dpi=300)

0 commit comments

Comments
 (0)