|
| 1 | +""" |
| 2 | +ridgeline-basic: Ridgeline Plot |
| 3 | +Library: letsplot |
| 4 | +""" |
| 5 | + |
| 6 | +import os |
| 7 | +import shutil |
| 8 | + |
| 9 | +import numpy as np |
| 10 | +import pandas as pd |
| 11 | +from lets_plot import * |
| 12 | + |
| 13 | + |
| 14 | +LetsPlot.setup_html() |
| 15 | + |
| 16 | +# Data - monthly temperature distributions |
| 17 | +np.random.seed(42) |
| 18 | +months = [ |
| 19 | + "January", |
| 20 | + "February", |
| 21 | + "March", |
| 22 | + "April", |
| 23 | + "May", |
| 24 | + "June", |
| 25 | + "July", |
| 26 | + "August", |
| 27 | + "September", |
| 28 | + "October", |
| 29 | + "November", |
| 30 | + "December", |
| 31 | +] |
| 32 | + |
| 33 | +# Generate realistic temperature data with seasonal pattern |
| 34 | +data = [] |
| 35 | +for i, month in enumerate(months): |
| 36 | + # Seasonal temperature pattern (Northern Hemisphere) |
| 37 | + base_temp = 5 + 15 * np.sin((i - 3) * np.pi / 6) |
| 38 | + temps = np.random.normal(base_temp, 4, 200) |
| 39 | + for temp in temps: |
| 40 | + data.append({"month": month, "temperature": temp}) |
| 41 | + |
| 42 | +df = pd.DataFrame(data) |
| 43 | + |
| 44 | +# Create ordered categorical for proper month ordering |
| 45 | +df["month"] = pd.Categorical(df["month"], categories=months, ordered=True) |
| 46 | + |
| 47 | +# Color palette gradient from cool to warm |
| 48 | +colors = [ |
| 49 | + "#306998", # Python Blue (winter) |
| 50 | + "#3B82F6", # Blue |
| 51 | + "#06B6D4", # Cyan |
| 52 | + "#10B981", # Emerald |
| 53 | + "#22C55E", # Green |
| 54 | + "#84CC16", # Lime |
| 55 | + "#FFD43B", # Python Yellow (summer) |
| 56 | + "#F97316", # Orange |
| 57 | + "#EF4444", # Red |
| 58 | + "#F97316", # Orange |
| 59 | + "#8B5CF6", # Violet |
| 60 | + "#306998", # Python Blue |
| 61 | +] |
| 62 | + |
| 63 | +# Create ridgeline plot using geom_area_ridges |
| 64 | +plot = ( |
| 65 | + ggplot(df, aes(x="temperature", y="month", fill="month")) |
| 66 | + + geom_area_ridges(alpha=0.7, scale=1.5, color="white", size=0.5) |
| 67 | + + scale_fill_manual(values=colors) |
| 68 | + + labs(x="Temperature (°C)", y="Month", title="Monthly Temperature Distribution") |
| 69 | + + theme_minimal() |
| 70 | + + theme( |
| 71 | + axis_text=element_text(size=16), |
| 72 | + axis_title=element_text(size=20), |
| 73 | + plot_title=element_text(size=20), |
| 74 | + legend_position="none", |
| 75 | + ) |
| 76 | + + ggsize(1600, 900) |
| 77 | +) |
| 78 | + |
| 79 | +# Save as PNG and HTML |
| 80 | +# Note: lets_plot saves to lets-plot-images/ subdirectory by default |
| 81 | +ggsave(plot, "plot.png", scale=3) |
| 82 | +ggsave(plot, "plot.html") |
| 83 | + |
| 84 | +# Move files from lets-plot-images to current directory |
| 85 | +if os.path.exists("lets-plot-images/plot.png"): |
| 86 | + shutil.move("lets-plot-images/plot.png", "plot.png") |
| 87 | +if os.path.exists("lets-plot-images/plot.html"): |
| 88 | + shutil.move("lets-plot-images/plot.html", "plot.html") |
| 89 | +if os.path.exists("lets-plot-images") and not os.listdir("lets-plot-images"): |
| 90 | + os.rmdir("lets-plot-images") |
0 commit comments