|
1 | | -""" pyplots.ai |
| 1 | +"""pyplots.ai |
2 | 2 | band-basic: Basic Band Plot |
3 | | -Library: letsplot 4.8.2 | Python 3.13.11 |
4 | | -Quality: 93/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 | 8 | import pandas as pd |
9 | 9 | from lets_plot import ( |
10 | 10 | LetsPlot, |
11 | 11 | aes, |
| 12 | + element_blank, |
12 | 13 | element_line, |
13 | 14 | element_text, |
14 | 15 | geom_line, |
|
17 | 18 | ggsave, |
18 | 19 | ggsize, |
19 | 20 | labs, |
| 21 | + layer_tooltips, |
20 | 22 | theme, |
21 | 23 | theme_minimal, |
22 | 24 | ) |
23 | 25 |
|
24 | 26 |
|
25 | 27 | LetsPlot.setup_html() |
26 | 28 |
|
27 | | -# Data - time series with 95% confidence interval |
| 29 | +# Data - sensor temperature readings with 95% confidence interval |
28 | 30 | np.random.seed(42) |
29 | | -x = np.linspace(0, 10, 100) |
30 | | -y_center = 2 * np.sin(x) + 0.5 * x # Central trend (sinusoidal + linear growth) |
31 | | -noise_scale = 0.3 + 0.15 * x # Increasing uncertainty over time |
32 | | -y_lower = y_center - 1.96 * noise_scale # 95% CI lower bound |
33 | | -y_upper = y_center + 1.96 * noise_scale # 95% CI upper bound |
| 31 | +time_seconds = np.linspace(0, 10, 100) |
| 32 | +temp_mean = 2 * np.sin(time_seconds) + 0.5 * time_seconds # Central trend |
| 33 | +uncertainty = 0.3 + 0.15 * time_seconds # Growing uncertainty over time |
| 34 | +temp_lower = temp_mean - 1.96 * uncertainty # 95% CI lower bound |
| 35 | +temp_upper = temp_mean + 1.96 * uncertainty # 95% CI upper bound |
34 | 36 |
|
35 | | -df = pd.DataFrame({"x": x, "y_center": y_center, "y_lower": y_lower, "y_upper": y_upper}) |
| 37 | +df = pd.DataFrame({"time": time_seconds, "mean": temp_mean, "lower": temp_lower, "upper": temp_upper}) |
36 | 38 |
|
37 | 39 | # Plot |
38 | 40 | plot = ( |
39 | | - ggplot(df, aes(x="x")) |
40 | | - + geom_ribbon(aes(ymin="y_lower", ymax="y_upper"), fill="#306998", alpha=0.3) |
41 | | - + geom_line(aes(y="y_center"), color="#306998", size=1.5) |
42 | | - + labs(x="Time (s)", y="Value (units)", title="band-basic · letsplot · pyplots.ai") |
| 41 | + ggplot(df, aes(x="time")) |
| 42 | + + geom_ribbon( |
| 43 | + aes(ymin="lower", ymax="upper"), |
| 44 | + fill="#306998", |
| 45 | + alpha=0.25, |
| 46 | + tooltips=layer_tooltips() |
| 47 | + .format("lower", "{.2f}") |
| 48 | + .format("upper", "{.2f}") |
| 49 | + .line("95% CI") |
| 50 | + .line("Upper|@upper") |
| 51 | + .line("Lower|@lower"), |
| 52 | + ) |
| 53 | + + geom_line( |
| 54 | + aes(y="mean"), |
| 55 | + color="#306998", |
| 56 | + size=1.5, |
| 57 | + tooltips=layer_tooltips() |
| 58 | + .format("mean", "{.2f}") |
| 59 | + .format("time", "{.1f}") |
| 60 | + .line("Time|@time s") |
| 61 | + .line("Mean|@mean"), |
| 62 | + ) |
| 63 | + + labs(x="Time (s)", y="Value (units)", title="band-basic \u00b7 letsplot \u00b7 pyplots.ai") |
43 | 64 | + theme_minimal() |
44 | 65 | + theme( |
45 | 66 | axis_title=element_text(size=20), |
46 | 67 | axis_text=element_text(size=16), |
47 | 68 | plot_title=element_text(size=24), |
48 | | - panel_grid=element_line(color="#cccccc", size=0.5), |
| 69 | + panel_grid_major=element_line(size=0.3, color="#E0E0E0"), |
| 70 | + panel_grid_minor=element_blank(), |
49 | 71 | ) |
50 | 72 | + ggsize(1600, 900) |
51 | 73 | ) |
52 | 74 |
|
53 | | -# Save PNG (scale=3 gives 4800x2700) |
| 75 | +# Save |
54 | 76 | ggsave(plot, "plot.png", path=".", scale=3) |
55 | | - |
56 | | -# Save HTML for interactive version |
57 | | -ggsave(plot, "plot.html", path=".") |
| 77 | +plot.to_html("plot.html") |
0 commit comments