-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathletsplot.py
More file actions
90 lines (78 loc) · 2.32 KB
/
letsplot.py
File metadata and controls
90 lines (78 loc) · 2.32 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
"""
ridgeline-basic: Ridgeline Plot
Library: letsplot
"""
import os
import shutil
import numpy as np
import pandas as pd
from lets_plot import *
LetsPlot.setup_html()
# 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 = []
for i, month in enumerate(months):
# Seasonal temperature pattern (Northern Hemisphere)
base_temp = 5 + 15 * np.sin((i - 3) * np.pi / 6)
temps = np.random.normal(base_temp, 4, 200)
for temp in temps:
data.append({"month": month, "temperature": temp})
df = pd.DataFrame(data)
# Create ordered categorical for proper month ordering
df["month"] = pd.Categorical(df["month"], categories=months, ordered=True)
# Color palette gradient from cool to warm
colors = [
"#306998", # Python Blue (winter)
"#3B82F6", # Blue
"#06B6D4", # Cyan
"#10B981", # Emerald
"#22C55E", # Green
"#84CC16", # Lime
"#FFD43B", # Python Yellow (summer)
"#F97316", # Orange
"#EF4444", # Red
"#F97316", # Orange
"#8B5CF6", # Violet
"#306998", # Python Blue
]
# Create ridgeline plot using geom_area_ridges
plot = (
ggplot(df, aes(x="temperature", y="month", fill="month"))
+ geom_area_ridges(alpha=0.7, scale=1.5, color="white", size=0.5)
+ scale_fill_manual(values=colors)
+ labs(x="Temperature (°C)", y="Month", title="Monthly Temperature Distribution")
+ theme_minimal()
+ theme(
axis_text=element_text(size=16),
axis_title=element_text(size=20),
plot_title=element_text(size=20),
legend_position="none",
)
+ ggsize(1600, 900)
)
# Save as PNG and HTML
# Note: lets_plot saves to lets-plot-images/ subdirectory by default
ggsave(plot, "plot.png", scale=3)
ggsave(plot, "plot.html")
# Move files from lets-plot-images to current directory
if os.path.exists("lets-plot-images/plot.png"):
shutil.move("lets-plot-images/plot.png", "plot.png")
if os.path.exists("lets-plot-images/plot.html"):
shutil.move("lets-plot-images/plot.html", "plot.html")
if os.path.exists("lets-plot-images") and not os.listdir("lets-plot-images"):
os.rmdir("lets-plot-images")