Skip to content

Commit e941886

Browse files
feat(bokeh): implement histogram-stacked (#2634)
## Implementation: `histogram-stacked` - bokeh Implements the **bokeh** version of `histogram-stacked`. **File:** `plots/histogram-stacked/implementations/bokeh.py` --- :robot: *[impl-generate workflow](https://github.com/MarkusNeusinger/pyplots/actions/runs/20594559186)* --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 99a3c9b commit e941886

2 files changed

Lines changed: 139 additions & 0 deletions

File tree

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
""" pyplots.ai
2+
histogram-stacked: Stacked Histogram
3+
Library: bokeh 3.8.1 | Python 3.13.11
4+
Quality: 91/100 | Created: 2025-12-30
5+
"""
6+
7+
import numpy as np
8+
from bokeh.io import export_png
9+
from bokeh.plotting import figure
10+
11+
12+
# Data - Simulating test scores from three different study groups
13+
np.random.seed(42)
14+
15+
# Group A: Regular study group (higher scores, tighter distribution)
16+
group_a = np.random.normal(loc=75, scale=8, size=150)
17+
18+
# Group B: Intensive study group (highest scores)
19+
group_b = np.random.normal(loc=82, scale=10, size=120)
20+
21+
# Group C: Self-study group (lower scores, wider distribution)
22+
group_c = np.random.normal(loc=65, scale=12, size=100)
23+
24+
# Create consistent bin edges for all groups
25+
all_data = np.concatenate([group_a, group_b, group_c])
26+
bin_edges = np.linspace(all_data.min() - 1, all_data.max() + 1, 16)
27+
28+
# Compute histograms with same bins
29+
hist_a, _ = np.histogram(group_a, bins=bin_edges)
30+
hist_b, _ = np.histogram(group_b, bins=bin_edges)
31+
hist_c, _ = np.histogram(group_c, bins=bin_edges)
32+
33+
# Calculate bar positions and widths
34+
bin_centers = (bin_edges[:-1] + bin_edges[1:]) / 2
35+
bin_width = bin_edges[1] - bin_edges[0]
36+
37+
# Create figure
38+
p = figure(
39+
width=4800,
40+
height=2700,
41+
title="histogram-stacked · bokeh · pyplots.ai",
42+
x_axis_label="Test Score (points)",
43+
y_axis_label="Frequency (count)",
44+
)
45+
46+
# Plot stacked bars - bottom to top: C, A, B
47+
# Group C at the bottom (Self-study - lower scores)
48+
p.vbar(
49+
x=bin_centers,
50+
top=hist_c,
51+
bottom=0,
52+
width=bin_width * 0.85,
53+
fill_color="#306998",
54+
line_color="white",
55+
line_width=2,
56+
alpha=0.9,
57+
legend_label="Self-study Group",
58+
)
59+
60+
# Group A stacked on top of C (Regular study - middle scores)
61+
p.vbar(
62+
x=bin_centers,
63+
top=hist_c + hist_a,
64+
bottom=hist_c,
65+
width=bin_width * 0.85,
66+
fill_color="#FFD43B",
67+
line_color="white",
68+
line_width=2,
69+
alpha=0.9,
70+
legend_label="Regular Study Group",
71+
)
72+
73+
# Group B stacked on top of A (Intensive study - higher scores)
74+
p.vbar(
75+
x=bin_centers,
76+
top=hist_c + hist_a + hist_b,
77+
bottom=hist_c + hist_a,
78+
width=bin_width * 0.85,
79+
fill_color="#4B8BBE",
80+
line_color="white",
81+
line_width=2,
82+
alpha=0.9,
83+
legend_label="Intensive Study Group",
84+
)
85+
86+
# Styling for large canvas (4800x2700)
87+
p.title.text_font_size = "36pt"
88+
p.xaxis.axis_label_text_font_size = "28pt"
89+
p.yaxis.axis_label_text_font_size = "28pt"
90+
p.xaxis.major_label_text_font_size = "22pt"
91+
p.yaxis.major_label_text_font_size = "22pt"
92+
93+
# Grid styling
94+
p.xgrid.grid_line_alpha = 0.3
95+
p.ygrid.grid_line_alpha = 0.3
96+
p.xgrid.grid_line_dash = "dashed"
97+
p.ygrid.grid_line_dash = "dashed"
98+
99+
# Legend styling - inside plot area
100+
p.legend.location = "top_right"
101+
p.legend.label_text_font_size = "22pt"
102+
p.legend.glyph_width = 50
103+
p.legend.glyph_height = 50
104+
p.legend.spacing = 15
105+
p.legend.padding = 25
106+
p.legend.background_fill_alpha = 0.85
107+
p.legend.border_line_width = 2
108+
p.legend.border_line_color = "#cccccc"
109+
110+
# Axis range padding
111+
p.y_range.start = 0
112+
113+
# Save
114+
export_png(p, filename="plot.png")
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
library: bokeh
2+
specification_id: histogram-stacked
3+
created: '2025-12-30T10:40:06Z'
4+
updated: '2025-12-30T10:50:22Z'
5+
generated_by: claude-opus-4-5-20251101
6+
workflow_run: 20594559186
7+
issue: 0
8+
python_version: 3.13.11
9+
library_version: 3.8.1
10+
preview_url: https://storage.googleapis.com/pyplots-images/plots/histogram-stacked/bokeh/plot.png
11+
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/histogram-stacked/bokeh/plot_thumb.png
12+
preview_html: https://storage.googleapis.com/pyplots-images/plots/histogram-stacked/bokeh/plot.html
13+
quality_score: 91
14+
review:
15+
strengths:
16+
- Excellent text sizing scaled appropriately for 4800x2700 canvas
17+
- Clean stacking implementation with proper bottom/top calculations
18+
- White line separators between segments improve readability
19+
- Realistic educational scenario with meaningful group differences
20+
- Proper use of consistent bin edges across all groups
21+
weaknesses:
22+
- 'Two blue colors (dark #306998 and light #4B8BBE) may cause confusion for colorblind
23+
users; consider using more distinct hues'
24+
- Does not use ColumnDataSource which is idiomatic Bokeh practice
25+
- Some test scores exceed 100 points which is atypical for standard tests

0 commit comments

Comments
 (0)