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