|
| 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") |
0 commit comments