|
1 | | -""" pyplots.ai |
| 1 | +""" anyplot.ai |
2 | 2 | scatter-basic: Basic Scatter Plot |
3 | | -Library: plotnine 0.15.3 | Python 3.14 |
4 | | -Quality: 93/100 | Created: 2025-12-22 |
| 3 | +Library: plotnine 0.15.3 | Python 3.14.4 |
| 4 | +Quality: 87/100 | Created: 2026-04-23 |
5 | 5 | """ |
6 | 6 |
|
| 7 | +import os |
| 8 | + |
7 | 9 | import numpy as np |
8 | 10 | import pandas as pd |
9 | 11 | from plotnine import ( |
10 | 12 | aes, |
11 | | - annotate, |
12 | 13 | element_blank, |
13 | 14 | element_line, |
14 | 15 | element_rect, |
15 | 16 | element_text, |
16 | 17 | geom_point, |
17 | 18 | ggplot, |
18 | 19 | labs, |
19 | | - scale_x_continuous, |
20 | | - scale_y_continuous, |
21 | | - stat_smooth, |
22 | 20 | theme, |
23 | 21 | theme_minimal, |
24 | 22 | ) |
25 | 23 |
|
26 | 24 |
|
27 | | -# Data: Study hours vs exam scores (realistic educational context) |
| 25 | +# Theme tokens |
| 26 | +THEME = os.getenv("ANYPLOT_THEME", "light") |
| 27 | +PAGE_BG = "#FAF8F1" if THEME == "light" else "#1A1A17" |
| 28 | +INK = "#1A1A17" if THEME == "light" else "#F0EFE8" |
| 29 | +INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0" |
| 30 | +GRID = INK |
| 31 | +GRID_ALPHA = 0.10 |
| 32 | +BRAND = "#009E73" |
| 33 | + |
| 34 | +# Data |
28 | 35 | np.random.seed(42) |
29 | | -n_points = 150 |
| 36 | +n_points = 180 |
30 | 37 | study_hours = np.random.uniform(1, 10, n_points) |
31 | | -base_scores = 35 + study_hours * 6 |
| 38 | +base_scores = 32 + study_hours * 6 |
32 | 39 | noise = np.random.randn(n_points) * 7 |
33 | 40 | exam_scores = np.clip(base_scores + noise, 15, 100) |
34 | | - |
35 | 41 | df = pd.DataFrame({"study_hours": study_hours, "exam_scores": exam_scores}) |
36 | 42 |
|
37 | | -# Compute correlation for annotation |
38 | | -correlation = df["study_hours"].corr(df["exam_scores"]) |
39 | | - |
40 | 43 | # Plot |
41 | 44 | plot = ( |
42 | 45 | ggplot(df, aes(x="study_hours", y="exam_scores")) |
43 | | - + geom_point(fill="#306998", color="#1a3a5c", shape="o", alpha=0.65, size=5, stroke=0.3) |
44 | | - + stat_smooth(method="lm", color="#e3822a", size=1.2, alpha=0.15, linetype="solid") |
45 | | - + annotate("text", x=2.0, y=97, label=f"r = {correlation:.2f}", size=14, color="#444444", fontstyle="italic") |
46 | | - + annotate("text", x=2.0, y=92, label="Strong positive correlation", size=10, color="#777777") |
47 | | - + scale_x_continuous(breaks=range(1, 11), limits=(0.5, 10.5)) |
48 | | - + scale_y_continuous(breaks=range(20, 101, 10), limits=(10, 105)) |
49 | | - + labs( |
50 | | - x="Study Hours (per week)", |
51 | | - y="Exam Score (points)", |
52 | | - title="scatter-basic · plotnine · pyplots.ai", |
53 | | - subtitle="More study time correlates with higher exam performance", |
54 | | - ) |
| 46 | + + geom_point(color=BRAND, fill=BRAND, alpha=0.70, size=4.0, stroke=0.4) |
| 47 | + + labs(x="Study Hours (per week)", y="Exam Score (points)", title="scatter-basic · plotnine · anyplot.ai") |
55 | 48 | + theme_minimal() |
56 | 49 | + theme( |
57 | 50 | figure_size=(16, 9), |
58 | | - text=element_text(size=14, color="#333333"), |
59 | | - axis_title=element_text(size=20, weight="bold", color="#222222"), |
60 | | - axis_text=element_text(size=16, color="#555555"), |
61 | | - plot_title=element_text(size=24, weight="bold", color="#1a1a1a"), |
62 | | - plot_subtitle=element_text(size=17, color="#666666"), |
63 | | - panel_grid_major=element_line(color="#e0e0e0", size=0.4), |
| 51 | + text=element_text(size=14, color=INK_SOFT), |
| 52 | + plot_title=element_text(size=24, color=INK, ha="left", margin={"b": 16}), |
| 53 | + axis_title_x=element_text(size=20, color=INK, margin={"t": 12}), |
| 54 | + axis_title_y=element_text(size=20, color=INK, margin={"r": 12}), |
| 55 | + axis_text=element_text(size=16, color=INK_SOFT), |
| 56 | + axis_ticks=element_blank(), |
| 57 | + axis_line=element_line(color=INK_SOFT, size=0.6), |
| 58 | + panel_grid_major=element_line(color=GRID, size=0.4, alpha=GRID_ALPHA), |
64 | 59 | panel_grid_minor=element_blank(), |
65 | | - plot_background=element_rect(fill="white", color="white"), |
66 | | - panel_background=element_rect(fill="#fafafa", color="white"), |
| 60 | + plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG), |
| 61 | + panel_background=element_rect(fill=PAGE_BG, color=PAGE_BG), |
67 | 62 | ) |
68 | 63 | ) |
69 | 64 |
|
70 | 65 | # Save |
71 | | -plot.save("plot.png", dpi=300, verbose=False) |
| 66 | +plot.save(f"plot-{THEME}.png", dpi=300, verbose=False) |
0 commit comments