|
1 | 1 | """ pyplots.ai |
2 | 2 | bubble-basic: Basic Bubble Chart |
3 | | -Library: bokeh 3.8.1 | Python 3.13.11 |
4 | | -Quality: 91/100 | Created: 2025-12-14 |
| 3 | +Library: bokeh 3.8.2 | Python 3.14.3 |
| 4 | +Quality: 93/100 | Created: 2026-02-16 |
5 | 5 | """ |
6 | 6 |
|
7 | 7 | import numpy as np |
8 | 8 | from bokeh.io import export_png |
9 | | -from bokeh.models import ColumnDataSource, Label |
| 9 | +from bokeh.models import BoxAnnotation, ColumnDataSource, HoverTool, Label, LinearColorMapper, Range1d |
| 10 | +from bokeh.palettes import Viridis256 |
10 | 11 | from bokeh.plotting import figure, output_file, save |
| 12 | +from bokeh.transform import transform |
11 | 13 |
|
12 | 14 |
|
13 | | -# Data |
| 15 | +# Data — City metrics: population density vs median income, bubble = green space per capita |
14 | 16 | np.random.seed(42) |
15 | | -n_points = 50 |
16 | | -x = np.random.randn(n_points) * 2 + 10 |
17 | | -y = x * 0.6 + np.random.randn(n_points) * 1.5 + 5 |
18 | | -size_raw = np.abs(np.random.randn(n_points) * 30 + 50) |
19 | | - |
20 | | -# Scale sizes for bubble area perception |
21 | | -# Map to visible range for 4800x2700 canvas |
22 | | -size_min, size_max = 30, 120 |
23 | | -size_scaled = size_min + (size_max - size_min) * (size_raw - size_raw.min()) / (size_raw.max() - size_raw.min()) |
24 | | - |
25 | | -source = ColumnDataSource(data={"x": x, "y": y, "size": size_scaled}) |
| 17 | +n_cities = 40 |
| 18 | + |
| 19 | +population_density = np.random.uniform(500, 12000, n_cities) # people per km² |
| 20 | +median_income = 30 + population_density / 400 + np.random.normal(0, 4, n_cities) # thousands USD |
| 21 | +green_space = np.random.uniform(5, 60, n_cities) # m² per capita |
| 22 | + |
| 23 | +# Scale bubble sizes by area for accurate perception (spec requirement) |
| 24 | +size_min, size_max = 25, 110 |
| 25 | +green_normalized = (green_space - green_space.min()) / (green_space.max() - green_space.min()) |
| 26 | +bubble_size = size_min + (size_max - size_min) * green_normalized |
| 27 | + |
| 28 | +# Color by green space — tells the story: denser cities tend to have less green space |
| 29 | +color_mapper = LinearColorMapper(palette=Viridis256, low=green_space.min(), high=green_space.max()) |
| 30 | + |
| 31 | +source = ColumnDataSource( |
| 32 | + data={ |
| 33 | + "density": population_density, |
| 34 | + "income": median_income, |
| 35 | + "size": bubble_size, |
| 36 | + "green_space": green_space, |
| 37 | + "density_display": np.round(population_density).astype(int), |
| 38 | + "income_display": np.round(median_income, 1), |
| 39 | + "green_display": np.round(green_space, 1), |
| 40 | + } |
| 41 | +) |
26 | 42 |
|
27 | 43 | # Plot |
28 | 44 | p = figure( |
29 | | - width=4800, height=2700, title="bubble-basic · bokeh · pyplots.ai", x_axis_label="X Value", y_axis_label="Y Value" |
| 45 | + width=4800, |
| 46 | + height=2700, |
| 47 | + title="bubble-basic · bokeh · pyplots.ai", |
| 48 | + x_axis_label="Population Density (people/km²)", |
| 49 | + y_axis_label="Median Income (thousands USD)", |
| 50 | + toolbar_location=None, |
30 | 51 | ) |
31 | 52 |
|
32 | | -# Create bubble scatter |
33 | 53 | p.scatter( |
34 | | - x="x", |
35 | | - y="y", |
| 54 | + x="density", |
| 55 | + y="income", |
36 | 56 | size="size", |
37 | 57 | source=source, |
38 | | - fill_color="#306998", |
39 | | - fill_alpha=0.6, |
40 | | - line_color="#306998", |
41 | | - line_alpha=0.8, |
| 58 | + fill_color=transform("green_space", color_mapper), |
| 59 | + fill_alpha=0.7, |
| 60 | + line_color="white", |
42 | 61 | line_width=2, |
43 | 62 | ) |
44 | 63 |
|
45 | | -# Styling (scaled for 4800x2700 px canvas) |
46 | | -p.title.text_font_size = "36pt" |
47 | | -p.xaxis.axis_label_text_font_size = "24pt" |
48 | | -p.yaxis.axis_label_text_font_size = "24pt" |
| 64 | +# Interactive hover tool — Bokeh-distinctive feature |
| 65 | +hover = HoverTool( |
| 66 | + tooltips=[ |
| 67 | + ("Density", "@density_display{,} people/km²"), |
| 68 | + ("Income", "$@income_display{0.0}k"), |
| 69 | + ("Green Space", "@green_display m²/capita"), |
| 70 | + ], |
| 71 | + mode="mouse", |
| 72 | +) |
| 73 | +p.add_tools(hover) |
| 74 | + |
| 75 | +# Typography (scaled for 4800x2700 px canvas) |
| 76 | +p.title.text_font_size = "30pt" |
| 77 | +p.title.text_color = "#222222" |
| 78 | +p.xaxis.axis_label_text_font_size = "22pt" |
| 79 | +p.yaxis.axis_label_text_font_size = "22pt" |
49 | 80 | p.xaxis.major_label_text_font_size = "18pt" |
50 | 81 | p.yaxis.major_label_text_font_size = "18pt" |
51 | | - |
52 | | -# Grid styling |
53 | | -p.grid.grid_line_alpha = 0.3 |
54 | | -p.grid.grid_line_dash = "dashed" |
55 | | - |
56 | | -# Add size legend (reference bubbles) in upper right area |
57 | | -legend_x = [max(x) + 1.5] * 3 |
58 | | -legend_y_positions = [max(y) - 0.5, max(y) - 2.5, max(y) - 5] |
59 | | -legend_sizes = [size_min, (size_min + size_max) / 2, size_max] |
60 | | -legend_labels = ["Small", "Medium", "Large"] |
61 | | - |
62 | | -legend_source = ColumnDataSource(data={"x": legend_x, "y": legend_y_positions, "size": legend_sizes}) |
63 | | - |
64 | | -p.scatter( |
65 | | - x="x", |
66 | | - y="y", |
67 | | - size="size", |
68 | | - source=legend_source, |
69 | | - fill_color="#306998", |
70 | | - fill_alpha=0.6, |
71 | | - line_color="#306998", |
72 | | - line_alpha=0.8, |
73 | | - line_width=2, |
| 82 | +p.xaxis.axis_label_text_color = "#444444" |
| 83 | +p.yaxis.axis_label_text_color = "#444444" |
| 84 | + |
| 85 | +# Clean frame — remove spines, outline, and tick marks |
| 86 | +p.outline_line_color = None |
| 87 | +p.xaxis.axis_line_color = None |
| 88 | +p.yaxis.axis_line_color = None |
| 89 | +p.xaxis.minor_tick_line_color = None |
| 90 | +p.yaxis.minor_tick_line_color = None |
| 91 | +p.xaxis.major_tick_line_color = None |
| 92 | +p.yaxis.major_tick_line_color = None |
| 93 | + |
| 94 | +# Subtle grid |
| 95 | +p.xgrid.grid_line_alpha = 0.15 |
| 96 | +p.ygrid.grid_line_alpha = 0.15 |
| 97 | +p.xgrid.grid_line_color = "#aaaaaa" |
| 98 | +p.ygrid.grid_line_color = "#aaaaaa" |
| 99 | +p.xgrid.grid_line_dash = [4, 4] |
| 100 | +p.ygrid.grid_line_dash = [4, 4] |
| 101 | + |
| 102 | +# Ranges — fit data tightly |
| 103 | +x_pad = (population_density.max() - population_density.min()) * 0.06 |
| 104 | +y_pad = (median_income.max() - median_income.min()) * 0.08 |
| 105 | +x_start = population_density.min() - x_pad * 2 |
| 106 | +x_end = population_density.max() + x_pad * 4 # room for legend on right side |
| 107 | +y_start = median_income.min() - y_pad * 2 |
| 108 | +y_end = median_income.max() + y_pad * 1.5 |
| 109 | +p.x_range = Range1d(start=x_start, end=x_end) |
| 110 | +p.y_range = Range1d(start=y_start, end=y_end) |
| 111 | + |
| 112 | +# Size legend — placed in lower-right with semi-transparent background |
| 113 | +legend_x = population_density.max() * 0.85 |
| 114 | +legend_y_top = y_start + (y_end - y_start) * 0.32 |
| 115 | + |
| 116 | +ref_green = [green_space.min(), (green_space.min() + green_space.max()) / 2, green_space.max()] |
| 117 | +ref_sizes = [size_min, (size_min + size_max) / 2, size_max] |
| 118 | +ref_labels = [f"{v:.0f} m²/capita" for v in ref_green] |
| 119 | + |
| 120 | +# Semi-transparent box behind legend for clarity |
| 121 | +legend_box = BoxAnnotation( |
| 122 | + left=legend_x - 800, |
| 123 | + right=x_end - 100, |
| 124 | + top=legend_y_top + 4.5, |
| 125 | + bottom=legend_y_top - 2.5 * 3.2 - 1, |
| 126 | + fill_color="white", |
| 127 | + fill_alpha=0.75, |
| 128 | + line_color="#cccccc", |
| 129 | + line_alpha=0.5, |
| 130 | +) |
| 131 | +p.add_layout(legend_box) |
| 132 | + |
| 133 | +p.add_layout( |
| 134 | + Label( |
| 135 | + x=legend_x - 400, |
| 136 | + y=legend_y_top + 3, |
| 137 | + text="Green Space", |
| 138 | + text_font_size="20pt", |
| 139 | + text_font_style="bold", |
| 140 | + text_color="#333333", |
| 141 | + ) |
74 | 142 | ) |
75 | 143 |
|
76 | | -# Add text labels for size legend |
77 | | -for lx, ly, label in zip(legend_x, legend_y_positions, legend_labels, strict=True): |
78 | | - text_label = Label(x=lx + 1.2, y=ly, text=label, text_font_size="18pt", text_baseline="middle") |
79 | | - p.add_layout(text_label) |
80 | | - |
81 | | -# Add "Size" title for legend |
82 | | -size_title = Label(x=legend_x[0] - 0.3, y=max(y) + 1, text="Size", text_font_size="20pt", text_font_style="bold") |
83 | | -p.add_layout(size_title) |
84 | | - |
85 | | -# Adjust x_range to accommodate legend |
86 | | -p.x_range.end = max(x) + 4 |
| 144 | +for i, (sz, lbl, gv) in enumerate(zip(ref_sizes, ref_labels, ref_green, strict=True)): |
| 145 | + ly = legend_y_top - i * 3.2 |
| 146 | + ref_src = ColumnDataSource(data={"x": [legend_x], "y": [ly], "size": [sz], "green_space": [gv]}) |
| 147 | + p.scatter( |
| 148 | + x="x", |
| 149 | + y="y", |
| 150 | + size="size", |
| 151 | + source=ref_src, |
| 152 | + fill_color=transform("green_space", color_mapper), |
| 153 | + fill_alpha=0.7, |
| 154 | + line_color="white", |
| 155 | + line_width=2, |
| 156 | + ) |
| 157 | + p.add_layout( |
| 158 | + Label(x=legend_x + 700, y=ly, text=lbl, text_font_size="18pt", text_baseline="middle", text_color="#444444") |
| 159 | + ) |
| 160 | + |
| 161 | +p.background_fill_color = "#fafafa" |
| 162 | +p.border_fill_color = "white" |
87 | 163 |
|
88 | 164 | # Save as PNG |
89 | 165 | export_png(p, filename="plot.png") |
90 | 166 |
|
91 | | -# Save as HTML (interactive) |
| 167 | +# Save as HTML (interactive — leverages Bokeh's hover tooltips) |
92 | 168 | output_file("plot.html") |
93 | 169 | save(p) |
0 commit comments