|
1 | 1 | """ pyplots.ai |
2 | 2 | hexbin-basic: Basic Hexbin Plot |
3 | | -Library: letsplot 4.8.1 | Python 3.13.11 |
4 | | -Quality: 91/100 | Created: 2025-12-23 |
| 3 | +Library: letsplot 4.8.2 | Python 3.14.3 |
| 4 | +Quality: /100 | Updated: 2026-02-21 |
5 | 5 | """ |
6 | 6 |
|
7 | 7 | import numpy as np |
|
16 | 16 | ggplot, |
17 | 17 | ggsize, |
18 | 18 | labs, |
| 19 | + layer_tooltips, |
19 | 20 | scale_fill_viridis, |
20 | 21 | theme, |
21 | 22 | theme_minimal, |
|
25 | 26 |
|
26 | 27 | LetsPlot.setup_html() |
27 | 28 |
|
28 | | -# Data - Generate clustered bivariate distribution for density visualization |
| 29 | +# Data - Simulated GPS ping density across a metro area (km from city center) |
29 | 30 | np.random.seed(42) |
30 | 31 | n_points = 10000 |
31 | 32 |
|
32 | | -# Create multiple clusters to demonstrate density patterns in hexbin visualization |
33 | | -cluster1_x = np.random.randn(n_points // 2) * 1.5 + 3 |
34 | | -cluster1_y = np.random.randn(n_points // 2) * 1.5 + 3 |
35 | | -cluster2_x = np.random.randn(n_points // 3) * 1.2 - 2 |
36 | | -cluster2_y = np.random.randn(n_points // 3) * 1.2 + 1 |
37 | | -cluster3_x = np.random.randn(n_points // 6) * 0.6 + 0 |
38 | | -cluster3_y = np.random.randn(n_points // 6) * 0.6 - 3 |
| 33 | +# Downtown core - dense commercial district |
| 34 | +downtown_east = np.random.randn(n_points // 2) * 1.5 + 4 |
| 35 | +downtown_north = np.random.randn(n_points // 2) * 1.5 + 3 |
39 | 36 |
|
40 | | -x = np.concatenate([cluster1_x, cluster2_x, cluster3_x]) |
41 | | -y = np.concatenate([cluster1_y, cluster2_y, cluster3_y]) |
| 37 | +# University campus - moderate foot traffic |
| 38 | +campus_east = np.random.randn(n_points // 3) * 1.0 - 3 |
| 39 | +campus_north = np.random.randn(n_points // 3) * 1.0 + 1 |
42 | 40 |
|
43 | | -df = pd.DataFrame({"x": x, "y": y}) |
| 41 | +# Transit hub - tight cluster of commuters |
| 42 | +transit_east = np.random.randn(n_points // 6) * 0.5 + 0.5 |
| 43 | +transit_north = np.random.randn(n_points // 6) * 0.5 - 3.5 |
44 | 44 |
|
45 | | -# Plot - Hexagonal binning to reveal density patterns |
| 45 | +east_km = np.concatenate([downtown_east, campus_east, transit_east]) |
| 46 | +north_km = np.concatenate([downtown_north, campus_north, transit_north]) |
| 47 | + |
| 48 | +df = pd.DataFrame({"east_km": east_km, "north_km": north_km}) |
| 49 | + |
| 50 | +# Plot - Hexagonal binning to reveal pedestrian density hotspots |
46 | 51 | plot = ( |
47 | | - ggplot(df, aes(x="x", y="y")) |
48 | | - + geom_hex(bins=[35, 35]) |
49 | | - + scale_fill_viridis(name="Count", option="viridis") |
50 | | - + labs(x="X Coordinate", y="Y Coordinate", title="hexbin-basic · letsplot · pyplots.ai") |
| 52 | + ggplot(df, aes(x="east_km", y="north_km")) |
| 53 | + + geom_hex( |
| 54 | + aes(fill="..count.."), |
| 55 | + bins=[30, 30], |
| 56 | + color="#FFFFFF", |
| 57 | + size=0.3, |
| 58 | + tooltips=layer_tooltips() |
| 59 | + .title("Hex Bin") |
| 60 | + .line("pings|@..count..") |
| 61 | + .line("density|@..density..") |
| 62 | + .format("@..density..", ".3f"), |
| 63 | + ) |
| 64 | + + scale_fill_viridis(name="Ping Count", option="viridis") |
| 65 | + + labs( |
| 66 | + x="East-West (km from center)", |
| 67 | + y="North-South (km from center)", |
| 68 | + title="hexbin-basic \u00b7 letsplot \u00b7 pyplots.ai", |
| 69 | + ) |
51 | 70 | + theme_minimal() |
52 | 71 | + theme( |
53 | 72 | axis_title=element_text(size=22), |
54 | 73 | axis_text=element_text(size=18), |
55 | 74 | plot_title=element_text(size=26), |
56 | 75 | legend_text=element_text(size=16), |
57 | 76 | legend_title=element_text(size=18), |
58 | | - panel_grid=element_line(color="#CCCCCC", size=0.4, linetype="dashed"), |
| 77 | + panel_grid=element_line(color="#E0E0E0", size=0.3, linetype="dashed"), |
59 | 78 | panel_background=element_rect(fill="#FAFAFA"), |
60 | 79 | ) |
61 | 80 | + ggsize(1600, 900) |
|
64 | 83 | # Save PNG (scale=3 gives 4800x2700) |
65 | 84 | ggsave(plot, "plot.png", path=".", scale=3) |
66 | 85 |
|
67 | | -# Save HTML for interactivity |
| 86 | +# Save HTML for interactive tooltips |
68 | 87 | ggsave(plot, "plot.html", path=".") |
0 commit comments