|
1 | 1 | """ pyplots.ai |
2 | 2 | bubble-packed: Basic Packed Bubble Chart |
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.3 |
| 4 | +Quality: 89/100 | Updated: 2026-02-23 |
5 | 5 | """ |
6 | 6 |
|
7 | 7 | import numpy as np |
|
10 | 10 | LetsPlot, |
11 | 11 | aes, |
12 | 12 | coord_fixed, |
| 13 | + element_rect, |
13 | 14 | element_text, |
14 | 15 | geom_point, |
15 | 16 | geom_text, |
16 | 17 | ggplot, |
17 | 18 | ggsize, |
| 19 | + guide_legend, |
| 20 | + guides, |
18 | 21 | labs, |
19 | | - scale_color_manual, |
20 | | - scale_size, |
| 22 | + layer_tooltips, |
| 23 | + scale_fill_manual, |
| 24 | + scale_size_identity, |
21 | 25 | theme, |
22 | 26 | theme_void, |
23 | 27 | xlim, |
|
28 | 32 |
|
29 | 33 | LetsPlot.setup_html() |
30 | 34 |
|
31 | | -# Data - department budget allocation |
| 35 | +# Data - department budget allocation ($M) |
32 | 36 | np.random.seed(42) |
33 | 37 | categories = [ |
34 | 38 | "Engineering", |
|
48 | 52 | "Security", |
49 | 53 | ] |
50 | 54 | values = np.array([85, 62, 58, 45, 32, 48, 72, 38, 22, 55, 68, 35, 42, 28, 30]) |
51 | | -groups = [ |
| 55 | +divisions = [ |
52 | 56 | "Tech", |
53 | 57 | "Business", |
54 | 58 | "Business", |
|
66 | 70 | "Tech", |
67 | 71 | ] |
68 | 72 |
|
69 | | -# Circle packing using force simulation (inline) |
| 73 | +# Circle packing with group-based spatial clustering |
70 | 74 | n = len(values) |
71 | | -radii = np.sqrt(values / np.pi) * 3.5 # Scale by area for accurate visual perception |
| 75 | +radii = np.sqrt(values / np.pi) * 3.5 |
| 76 | +div_names = ["Tech", "Business", "Operations"] |
| 77 | +div_angles = {g: i * 2 * np.pi / len(div_names) for i, g in enumerate(div_names)} |
72 | 78 |
|
73 | | -# Initialize positions |
74 | 79 | np.random.seed(42) |
75 | | -x = np.random.uniform(-100, 100, n) |
76 | | -y = np.random.uniform(-100, 100, n) |
| 80 | +x = np.zeros(n, dtype=float) |
| 81 | +y = np.zeros(n, dtype=float) |
| 82 | +for i in range(n): |
| 83 | + angle = div_angles[divisions[i]] + np.random.uniform(-0.4, 0.4) |
| 84 | + r_init = np.random.uniform(3, 20) |
| 85 | + x[i] = r_init * np.cos(angle) |
| 86 | + y[i] = r_init * np.sin(angle) |
77 | 87 |
|
78 | | -# Force-directed packing simulation |
79 | | -for _ in range(600): |
80 | | - # Pull toward center |
81 | | - x *= 0.99 |
82 | | - y *= 0.99 |
| 88 | +# Force-directed packing: gravity, group attraction, and collision resolution |
| 89 | +for step in range(1700): |
| 90 | + if step < 1200: |
| 91 | + x *= 0.995 |
| 92 | + y *= 0.995 |
| 93 | + for g in div_names: |
| 94 | + mask = np.array([divisions[i] == g for i in range(n)]) |
| 95 | + if mask.sum() > 1: |
| 96 | + cx, cy = x[mask].mean(), y[mask].mean() |
| 97 | + x[mask] += (cx - x[mask]) * 0.025 |
| 98 | + y[mask] += (cy - y[mask]) * 0.025 |
83 | 99 |
|
84 | | - # Push overlapping circles apart |
| 100 | + settled = True |
85 | 101 | for i in range(n): |
86 | 102 | for j in range(i + 1, n): |
87 | 103 | dx = x[j] - x[i] |
88 | 104 | dy = y[j] - y[i] |
89 | 105 | dist = np.sqrt(dx * dx + dy * dy) |
90 | | - min_dist = radii[i] + radii[j] + 0.5 |
91 | | - |
| 106 | + spacing = 1.0 if divisions[i] != divisions[j] else 0.25 |
| 107 | + min_dist = radii[i] + radii[j] + spacing |
92 | 108 | if dist < min_dist and dist > 0: |
| 109 | + settled = False |
93 | 110 | overlap = (min_dist - dist) / 2 |
94 | | - move_x = (dx / dist) * overlap |
95 | | - move_y = (dy / dist) * overlap |
96 | | - x[i] -= move_x |
97 | | - y[i] -= move_y |
98 | | - x[j] += move_x |
99 | | - y[j] += move_y |
| 111 | + ux, uy = dx / dist, dy / dist |
| 112 | + x[i] -= ux * overlap |
| 113 | + y[i] -= uy * overlap |
| 114 | + x[j] += ux * overlap |
| 115 | + y[j] += uy * overlap |
| 116 | + |
| 117 | + if step >= 1200 and settled: |
| 118 | + break |
| 119 | + |
| 120 | +x -= x.mean() |
| 121 | +y -= y.mean() |
| 122 | + |
| 123 | +# Build DataFrame with diameter in data units for geom_point size_unit='x' |
| 124 | +abbrev = {"Customer Support": "Support", "Operations": "Ops"} |
| 125 | +df = pd.DataFrame( |
| 126 | + { |
| 127 | + "x": x, |
| 128 | + "y": y, |
| 129 | + "division": divisions, |
| 130 | + "label": categories, |
| 131 | + "budget": [f"${v}M" for v in values], |
| 132 | + "diameter": radii * 2, |
| 133 | + "display_label": [ |
| 134 | + (f"{abbrev.get(c, c)}\n${v}M" if v >= 48 else (abbrev.get(c, c) if v >= 35 else "")) |
| 135 | + for c, v in zip(categories, values, strict=True) |
| 136 | + ], |
| 137 | + } |
| 138 | +) |
100 | 139 |
|
101 | | -df = pd.DataFrame({"label": categories, "value": values, "group": groups, "x": x, "y": y}) |
| 140 | +# Axis limits ensuring all circles are fully visible |
| 141 | +x_lo = min(x[i] - radii[i] for i in range(n)) |
| 142 | +x_hi = max(x[i] + radii[i] for i in range(n)) |
| 143 | +y_lo = min(y[i] - radii[i] for i in range(n)) |
| 144 | +y_hi = max(y[i] + radii[i] for i in range(n)) |
| 145 | +pad = (x_hi - x_lo) * 0.03 |
102 | 146 |
|
103 | | -# Abbreviate long labels to fit inside bubbles |
104 | | -label_map = { |
105 | | - "Customer Support": "Support", |
106 | | - "Engineering": "Engineering", |
107 | | - "Marketing": "Marketing", |
108 | | - "Sales": "Sales", |
109 | | - "Operations": "Ops", |
110 | | - "Finance": "Finance", |
111 | | - "R&D": "R&D", |
112 | | - "HR": "HR", |
113 | | - "Legal": "Legal", |
114 | | - "IT": "IT", |
115 | | - "Product": "Product", |
116 | | - "Design": "Design", |
117 | | - "Analytics": "Analytics", |
118 | | - "QA": "QA", |
119 | | - "Security": "Security", |
120 | | -} |
121 | | -df["short_label"] = df["label"].map(label_map) |
| 147 | +# Colorblind-safe palette: Python Blue + Wong (verified for all CVD types) |
| 148 | +palette = {"Tech": "#306998", "Business": "#E69F00", "Operations": "#009E73"} |
122 | 149 |
|
123 | | -# Plot |
124 | 150 | plot = ( |
125 | | - ggplot(df, aes(x="x", y="y")) |
126 | | - + geom_point(aes(size="value", color="group"), alpha=0.85) |
127 | | - + geom_text(aes(label="short_label"), size=7, color="white", fontface="bold") |
128 | | - + scale_size(range=[20, 85], guide="none") |
129 | | - + scale_color_manual(values=["#FFD43B", "#4ECDC4", "#306998"]) |
130 | | - + labs(title="Department Budget Allocation · bubble-packed · letsplot · pyplots.ai", color="Division") |
131 | | - + xlim(-70, 70) |
132 | | - + ylim(-70, 55) |
| 151 | + ggplot(df) |
| 152 | + + geom_point( |
| 153 | + aes(x="x", y="y", fill="division", size="diameter"), |
| 154 | + shape=21, |
| 155 | + color="white", |
| 156 | + stroke=1.5, |
| 157 | + alpha=0.88, |
| 158 | + size_unit="x", |
| 159 | + tooltips=(layer_tooltips().title("@label").line("Budget|@budget").line("Division|@division")), |
| 160 | + ) |
| 161 | + + scale_size_identity(guide="none") |
| 162 | + + geom_text(aes(x="x", y="y", label="display_label"), size=9, color="white", fontface="bold") |
| 163 | + + scale_fill_manual(values=palette) |
| 164 | + + guides(fill=guide_legend(nrow=1)) |
| 165 | + + coord_fixed() |
| 166 | + + xlim(x_lo - pad, x_hi + pad) |
| 167 | + + ylim(y_lo - pad, y_hi + pad) |
| 168 | + + labs( |
| 169 | + title="Department Budget Allocation \u00b7 bubble-packed \u00b7 letsplot \u00b7 pyplots.ai", |
| 170 | + subtitle="Tech departments dominate \u2014 8 of 15 teams control 58% of total budget", |
| 171 | + fill="Division", |
| 172 | + ) |
133 | 173 | + theme_void() |
134 | 174 | + theme( |
135 | 175 | plot_title=element_text(size=24, hjust=0.5), |
136 | | - legend_position="right", |
137 | | - legend_title=element_text(size=18), |
| 176 | + plot_subtitle=element_text(size=16, hjust=0.5, color="#666666"), |
| 177 | + legend_position="bottom", |
| 178 | + legend_title=element_text(size=20), |
138 | 179 | legend_text=element_text(size=16), |
| 180 | + legend_background=element_rect(fill="white", color="#CCCCCC", size=0.5), |
139 | 181 | ) |
140 | | - + coord_fixed() |
141 | | - + ggsize(1600, 900) |
| 182 | + + ggsize(1200, 1200) |
142 | 183 | ) |
143 | 184 |
|
144 | | -# Save PNG (scale 3x to get 4800x2700 px) |
145 | 185 | export_ggsave(plot, "plot.png", path=".", scale=3) |
146 | | - |
147 | | -# Save HTML for interactive version |
148 | | -export_ggsave(plot, "plot.html", path=".") |
|
0 commit comments