|
1 | 1 | """ anyplot.ai |
2 | 2 | swarm-basic: Basic Swarm Plot |
3 | | -Library: letsplot 4.9.0 | Python 3.13.13 |
4 | | -Quality: 80/100 | Updated: 2026-05-05 |
| 3 | +Library: letsplot 4.11.0 | Python 3.13.14 |
| 4 | +Quality: 93/100 | Updated: 2026-07-26 |
5 | 5 | """ |
6 | 6 |
|
| 7 | +import os |
| 8 | + |
7 | 9 | import numpy as np |
8 | 10 | import pandas as pd |
9 | 11 | from lets_plot import ( |
10 | 12 | LetsPlot, |
11 | 13 | aes, |
12 | 14 | element_blank, |
13 | 15 | element_line, |
| 16 | + element_rect, |
14 | 17 | element_text, |
15 | 18 | geom_crossbar, |
16 | 19 | geom_sina, |
| 20 | + geom_violin, |
17 | 21 | ggplot, |
18 | 22 | ggsave, |
19 | 23 | ggsize, |
|
27 | 31 |
|
28 | 32 | LetsPlot.setup_html() |
29 | 33 |
|
| 34 | +# Theme-adaptive chrome tokens (Imprint palette + surface tokens) |
| 35 | +THEME = os.getenv("ANYPLOT_THEME", "light") |
| 36 | +PAGE_BG = "#FAF8F1" if THEME == "light" else "#1A1A17" |
| 37 | +ELEVATED_BG = "#FFFDF6" if THEME == "light" else "#242420" |
| 38 | +INK = "#1A1A17" if THEME == "light" else "#F0EFE8" |
| 39 | +INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0" |
| 40 | + |
| 41 | +IMPRINT_PALETTE = ["#009E73", "#C475FD", "#4467A3", "#BD8233"] |
| 42 | + |
30 | 43 | # Data - Performance scores across departments |
31 | 44 | np.random.seed(42) |
32 | 45 |
|
33 | 46 | departments = ["Engineering", "Marketing", "Sales", "Support"] |
34 | 47 | n_per_group = [45, 38, 52, 40] |
35 | | -colors = ["#306998", "#FFD43B", "#2E8B57", "#DC143C"] |
36 | 48 |
|
37 | 49 | data = [] |
38 | 50 | for dept, n in zip(departments, n_per_group, strict=True): |
|
59 | 71 | means = df.groupby("Department")["Performance Score"].mean().reset_index() |
60 | 72 | means.columns = ["Department", "mean"] |
61 | 73 |
|
| 74 | +title = "swarm-basic · python · letsplot · anyplot.ai" |
| 75 | + |
62 | 76 | # Plot |
63 | 77 | plot = ( |
64 | 78 | ggplot(df, aes(x="Department", y="Performance Score")) |
| 79 | + + geom_violin(aes(fill="Department"), alpha=0.15, trim=False, show_legend=False) |
65 | 80 | + geom_sina(aes(color="Department", fill="Department"), size=4, alpha=0.7, seed=42, scale="width") |
66 | | - + geom_crossbar( |
67 | | - aes(x="Department", y="mean", ymin="mean", ymax="mean"), data=means, width=0.5, size=1.5, color="#333333" |
68 | | - ) |
69 | | - + scale_color_manual(values=colors) |
70 | | - + scale_fill_manual(values=colors) |
71 | | - + labs(x="Department", y="Performance Score", title="swarm-basic · letsplot · pyplots.ai") |
| 81 | + + geom_crossbar(aes(x="Department", y="mean", ymin="mean", ymax="mean"), data=means, width=0.5, size=1.5, color=INK) |
| 82 | + + scale_color_manual(values=IMPRINT_PALETTE) |
| 83 | + + scale_fill_manual(values=IMPRINT_PALETTE) |
| 84 | + + labs(x="Department", y="Performance Score (0-100)", title=title) |
72 | 85 | + theme_minimal() |
73 | 86 | + theme( |
74 | | - plot_title=element_text(size=24, face="bold"), |
75 | | - axis_title=element_text(size=20), |
76 | | - axis_text=element_text(size=16), |
| 87 | + plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG), |
| 88 | + panel_background=element_rect(fill=PAGE_BG, color=PAGE_BG), |
| 89 | + panel_border=element_blank(), |
| 90 | + legend_background=element_rect(fill=ELEVATED_BG, color=INK_SOFT), |
| 91 | + plot_title=element_text(size=16, face="bold", color=INK), |
| 92 | + axis_title=element_text(size=12, color=INK), |
| 93 | + axis_text=element_text(size=10, color=INK_SOFT), |
| 94 | + axis_line=element_line(color=INK_SOFT), |
77 | 95 | legend_position="none", |
78 | 96 | panel_grid_major_x=element_blank(), |
79 | 97 | panel_grid_minor=element_blank(), |
80 | | - panel_grid_major_y=element_line(color="#cccccc", size=0.5), |
| 98 | + panel_grid_major_y=element_line(color=INK_SOFT, size=0.3), |
81 | 99 | ) |
82 | | - + ggsize(1600, 900) |
| 100 | + + ggsize(800, 450) |
83 | 101 | ) |
84 | 102 |
|
85 | | -# Save PNG (scale=3 gives 4800x2700) |
86 | | -ggsave(plot, "plot.png", path=".", scale=3) |
| 103 | +# Save PNG (scale=4 gives 3200x1800) |
| 104 | +ggsave(plot, f"plot-{THEME}.png", path=".", scale=4) |
87 | 105 |
|
88 | 106 | # Save HTML for interactive version |
89 | | -ggsave(plot, "plot.html", path=".") |
| 107 | +ggsave(plot, f"plot-{THEME}.html", path=".") |
0 commit comments