|
1 | 1 | """ pyplots.ai |
2 | 2 | density-basic: Basic Density Plot |
3 | | -Library: letsplot 4.8.2 | Python 3.13.11 |
4 | | -Quality: 91/100 | Created: 2025-12-23 |
| 3 | +Library: letsplot 4.8.2 | Python 3.14 |
| 4 | +Quality: /100 | Updated: 2026-02-23 |
5 | 5 | """ |
6 | 6 |
|
7 | 7 | import numpy as np |
| 8 | +import pandas as pd |
8 | 9 | from lets_plot import * # noqa: F403 |
9 | | -from lets_plot.export import ggsave as export_ggsave |
10 | 10 |
|
11 | 11 |
|
12 | 12 | LetsPlot.setup_html() # noqa: F405 |
13 | 13 |
|
14 | | -# Data - Generate realistic test scores with slight right skew |
| 14 | +# Data - Simulated marathon finish times with realistic right skew |
15 | 15 | np.random.seed(42) |
16 | | -scores = np.concatenate( |
| 16 | +finish_minutes = np.concatenate( |
17 | 17 | [ |
18 | | - np.random.normal(72, 12, 300), # Main group of students |
19 | | - np.random.normal(90, 5, 100), # High achievers |
| 18 | + np.random.normal(240, 25, 350), # Main pack (~4 hour runners) |
| 19 | + np.random.normal(200, 15, 100), # Competitive runners (~3:20) |
| 20 | + np.random.normal(300, 20, 50), # Casual runners (~5 hours) |
20 | 21 | ] |
21 | 22 | ) |
| 23 | +finish_minutes = np.clip(finish_minutes, 140, 400) |
22 | 24 |
|
23 | | -# Create plot |
| 25 | +df = pd.DataFrame({"time": finish_minutes}) |
| 26 | + |
| 27 | +# Plot |
24 | 28 | plot = ( |
25 | | - ggplot({"scores": scores}, aes(x="scores")) # noqa: F405 |
26 | | - + geom_density(fill="#306998", color="#306998", alpha=0.6, size=1.5) # noqa: F405 |
27 | | - + labs(x="Test Score", y="Density", title="density-basic · letsplot · pyplots.ai") # noqa: F405 |
| 29 | + ggplot(df, aes(x="time")) # noqa: F405 |
| 30 | + + geom_density( # noqa: F405 |
| 31 | + fill="#306998", |
| 32 | + color="#1e4263", |
| 33 | + alpha=0.55, |
| 34 | + size=1.8, |
| 35 | + kernel="gaussian", |
| 36 | + adjust=0.85, |
| 37 | + trim=True, |
| 38 | + tooltips=layer_tooltips() # noqa: F405 |
| 39 | + .line("@|@time") |
| 40 | + .line("density|@..density.."), |
| 41 | + ) |
| 42 | + + labs( # noqa: F405 |
| 43 | + x="Finish Time (minutes)", y="Density", title="density-basic · letsplot · pyplots.ai" |
| 44 | + ) |
| 45 | + + scale_x_continuous(breaks=list(range(150, 401, 50))) # noqa: F405 |
| 46 | + + scale_y_continuous(expand=[0.02, 0, 0.05, 0]) # noqa: F405 |
28 | 47 | + theme_minimal() # noqa: F405 |
29 | 48 | + theme( # noqa: F405 |
30 | 49 | axis_title=element_text(size=20), # noqa: F405 |
31 | 50 | axis_text=element_text(size=16), # noqa: F405 |
32 | 51 | plot_title=element_text(size=24), # noqa: F405 |
33 | | - panel_grid_major=element_line(color="#cccccc", size=0.5), # noqa: F405 |
| 52 | + panel_grid_major_x=element_blank(), # noqa: F405 |
| 53 | + panel_grid_major_y=element_line(color="#e0e0e0", size=0.4), # noqa: F405 |
34 | 54 | panel_grid_minor=element_blank(), # noqa: F405 |
| 55 | + axis_ticks=element_blank(), # noqa: F405 |
35 | 56 | ) |
36 | 57 | + ggsize(1600, 900) # noqa: F405 |
37 | 58 | ) |
38 | 59 |
|
39 | | -# Save PNG (scale 3x for 4800x2700) and HTML |
40 | | -export_ggsave(plot, "plot.png", path=".", scale=3) |
41 | | -export_ggsave(plot, "plot.html", path=".") |
| 60 | +# Save PNG (scale 3x for 4800 x 2700 px) and HTML |
| 61 | +ggsave(plot, "plot.png", path=".", scale=3) # noqa: F405 |
| 62 | +ggsave(plot, "plot.html", path=".") # noqa: F405 |
0 commit comments