|
1 | | -""" pyplots.ai |
| 1 | +""" anyplot.ai |
2 | 2 | step-basic: Basic Step Plot |
3 | | -Library: letsplot 4.8.2 | Python 3.13.11 |
4 | | -Quality: 92/100 | Created: 2025-12-23 |
| 3 | +Library: letsplot 4.9.0 | Python 3.13.13 |
| 4 | +Quality: 88/100 | Updated: 2026-04-30 |
5 | 5 | """ |
6 | 6 |
|
| 7 | +import os |
| 8 | + |
7 | 9 | import numpy as np |
8 | 10 | import pandas as pd |
9 | | -from lets_plot import * # noqa: F403 |
10 | | -from lets_plot.export import ggsave as export_ggsave |
| 11 | +from lets_plot import ( |
| 12 | + LetsPlot, |
| 13 | + aes, |
| 14 | + element_blank, |
| 15 | + element_line, |
| 16 | + element_rect, |
| 17 | + element_text, |
| 18 | + geom_area, |
| 19 | + geom_point, |
| 20 | + geom_step, |
| 21 | + geom_text, |
| 22 | + ggplot, |
| 23 | + ggsave, |
| 24 | + ggsize, |
| 25 | + labs, |
| 26 | + scale_x_continuous, |
| 27 | + theme, |
| 28 | + theme_minimal, |
| 29 | +) |
11 | 30 |
|
12 | 31 |
|
13 | | -LetsPlot.setup_html() # noqa: F405 |
| 32 | +LetsPlot.setup_html() |
| 33 | + |
| 34 | +# Theme tokens |
| 35 | +THEME = os.getenv("ANYPLOT_THEME", "light") |
| 36 | +PAGE_BG = "#FAF8F1" if THEME == "light" else "#1A1A17" |
| 37 | +INK = "#1A1A17" if THEME == "light" else "#F0EFE8" |
| 38 | +INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0" |
| 39 | +RULE = "rgba(26,26,23,0.10)" if THEME == "light" else "rgba(240,239,232,0.10)" |
| 40 | +BRAND = "#009E73" # Okabe-Ito position 1 — always first series |
14 | 41 |
|
15 | 42 | # Data - Monthly cumulative sales figures (in thousands) |
16 | 43 | np.random.seed(42) |
17 | 44 | months = np.arange(1, 13) |
18 | | -# Monthly sales that accumulate over the year |
19 | 45 | monthly_sales = np.array([45, 52, 48, 61, 55, 72, 68, 75, 82, 78, 91, 95]) |
20 | 46 | cumulative_sales = np.cumsum(monthly_sales) |
21 | 47 |
|
22 | 48 | df = pd.DataFrame({"month": months, "cumulative_sales": cumulative_sales}) |
23 | 49 |
|
24 | | -# Plot - Step plot showing cumulative values that remain constant between changes |
| 50 | +# Annotation: label the year-end total near the final data point |
| 51 | +total = int(cumulative_sales[-1]) |
| 52 | +label_df = pd.DataFrame({"month": [11.4], "cumulative_sales": [total + 52], "label": [f"Year-end total: ${total}K"]}) |
| 53 | + |
| 54 | +# Plot |
25 | 55 | plot = ( |
26 | | - ggplot(df, aes(x="month", y="cumulative_sales")) # noqa: F405 |
27 | | - + geom_step(color="#306998", size=2, direction="hv") # noqa: F405 |
28 | | - + geom_point(color="#FFD43B", size=5, stroke=1) # noqa: F405 |
29 | | - + labs( # noqa: F405 |
30 | | - x="Month", y="Cumulative Sales ($K)", title="step-basic · letsplot · pyplots.ai" |
| 56 | + ggplot(df, aes(x="month", y="cumulative_sales")) |
| 57 | + + geom_area(fill=BRAND, alpha=0.15) |
| 58 | + + geom_step(color=BRAND, size=2, direction="hv") |
| 59 | + + geom_point(color=BRAND, size=6, alpha=0.9) |
| 60 | + + geom_text( |
| 61 | + data=label_df, mapping=aes(x="month", y="cumulative_sales", label="label"), color=INK_SOFT, size=13, hjust=1 |
31 | 62 | ) |
32 | | - + scale_x_continuous(breaks=list(range(1, 13))) # noqa: F405 |
33 | | - + ggsize(1600, 900) # noqa: F405 |
34 | | - + theme_minimal() # noqa: F405 |
35 | | - + theme( # noqa: F405 |
36 | | - axis_text=element_text(size=16), # noqa: F405 |
37 | | - axis_title=element_text(size=20), # noqa: F405 |
38 | | - plot_title=element_text(size=24), # noqa: F405 |
39 | | - panel_grid=element_line(color="#CCCCCC", size=0.5, linetype="dashed"), # noqa: F405 |
| 63 | + + labs(x="Month", y="Cumulative Sales ($K)", title="step-basic · letsplot · anyplot.ai") |
| 64 | + + scale_x_continuous(breaks=list(range(1, 13))) |
| 65 | + + ggsize(1600, 900) |
| 66 | + + theme_minimal() |
| 67 | + + theme( |
| 68 | + plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG), |
| 69 | + panel_background=element_rect(fill=PAGE_BG), |
| 70 | + panel_grid_major_x=element_blank(), |
| 71 | + panel_grid_major_y=element_line(color=RULE, size=0.3), |
| 72 | + panel_grid_minor=element_blank(), |
| 73 | + axis_title=element_text(color=INK, size=20), |
| 74 | + axis_text=element_text(color=INK_SOFT, size=16), |
| 75 | + axis_line=element_line(color=INK_SOFT), |
| 76 | + plot_title=element_text(color=INK, size=24), |
40 | 77 | ) |
41 | 78 | ) |
42 | 79 |
|
43 | 80 | # Save PNG (scale 3x to get 4800 x 2700 px) |
44 | | -export_ggsave(plot, filename="plot.png", path=".", scale=3) |
| 81 | +ggsave(plot, f"plot-{THEME}.png", path=".", scale=3) |
45 | 82 |
|
46 | 83 | # Save HTML for interactive version |
47 | | -export_ggsave(plot, filename="plot.html", path=".") |
| 84 | +ggsave(plot, f"plot-{THEME}.html", path=".") |
0 commit comments