|
| 1 | +""" pyplots.ai |
| 2 | +point-basic: Point Estimate Plot |
| 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, save |
| 9 | +from bokeh.models import ColumnDataSource, Span, TeeHead, Whisker |
| 10 | +from bokeh.plotting import figure |
| 11 | +from bokeh.resources import CDN |
| 12 | + |
| 13 | + |
| 14 | +# Data - Effect sizes for different treatment groups with 95% confidence intervals |
| 15 | +np.random.seed(42) |
| 16 | +categories = ["Treatment A", "Treatment B", "Treatment C", "Treatment D", "Treatment E", "Control"] |
| 17 | +estimates = np.array([2.5, 1.8, 3.2, -0.5, 1.2, 0.0]) |
| 18 | +# Generate confidence intervals with varying widths |
| 19 | +ci_widths = np.array([0.8, 1.2, 0.6, 0.9, 1.5, 0.4]) |
| 20 | +lower = estimates - ci_widths |
| 21 | +upper = estimates + ci_widths |
| 22 | + |
| 23 | +# Create ColumnDataSource |
| 24 | +source = ColumnDataSource(data={"categories": categories, "estimates": estimates, "lower": lower, "upper": upper}) |
| 25 | + |
| 26 | +# Create figure with categorical y-axis (horizontal orientation for readability) |
| 27 | +p = figure( |
| 28 | + width=4800, |
| 29 | + height=2700, |
| 30 | + y_range=categories[::-1], # Reverse order so first category is at top |
| 31 | + title="point-basic · bokeh · pyplots.ai", |
| 32 | + x_axis_label="Effect Size", |
| 33 | + y_axis_label="Treatment Group", |
| 34 | +) |
| 35 | + |
| 36 | +# Add reference line at zero (null hypothesis) |
| 37 | +zero_line = Span(location=0, dimension="height", line_color="#888888", line_width=3, line_dash="dashed") |
| 38 | +p.add_layout(zero_line) |
| 39 | + |
| 40 | +# Add error bars using Whisker with TeeHead caps (horizontal) |
| 41 | +whisker = Whisker( |
| 42 | + source=source, |
| 43 | + base="categories", |
| 44 | + lower="lower", |
| 45 | + upper="upper", |
| 46 | + dimension="width", |
| 47 | + line_color="#306998", |
| 48 | + line_width=6, |
| 49 | + upper_head=TeeHead(size=30, line_color="#306998", line_width=6), |
| 50 | + lower_head=TeeHead(size=30, line_color="#306998", line_width=6), |
| 51 | +) |
| 52 | +p.add_layout(whisker) |
| 53 | + |
| 54 | +# Plot points (estimates) with larger markers for visibility |
| 55 | +p.scatter(x="estimates", y="categories", source=source, size=40, color="#306998", fill_color="#FFD43B", line_width=4) |
| 56 | + |
| 57 | +# Styling for large canvas |
| 58 | +p.title.text_font_size = "36pt" |
| 59 | +p.xaxis.axis_label_text_font_size = "28pt" |
| 60 | +p.yaxis.axis_label_text_font_size = "28pt" |
| 61 | +p.xaxis.major_label_text_font_size = "22pt" |
| 62 | +p.yaxis.major_label_text_font_size = "22pt" |
| 63 | + |
| 64 | +# Grid styling |
| 65 | +p.xgrid.grid_line_alpha = 0.3 |
| 66 | +p.xgrid.grid_line_dash = "dashed" |
| 67 | +p.ygrid.grid_line_alpha = 0.3 |
| 68 | +p.ygrid.grid_line_dash = "dashed" |
| 69 | + |
| 70 | +# Remove toolbar and outline for cleaner look |
| 71 | +p.toolbar_location = None |
| 72 | +p.outline_line_color = None |
| 73 | + |
| 74 | +# Save as PNG and HTML |
| 75 | +export_png(p, filename="plot.png") |
| 76 | +save(p, filename="plot.html", resources=CDN, title="point-basic · bokeh · pyplots.ai") |
0 commit comments