Accessible Matplotlib plots
Quick answer
Matplotlib separates the Figure (the whole drawing) from each Axes (one plotting area). Use plots to diagnose data and model behavior, not merely decorate a report. Histograms summarize counts by intervals; scatter plots expose paired observations; residual plots show prediction errors; error bars display explicitly defined uncertainty or variation. Include units, labels, readable markers and a textual equivalent. Examples use local Python 3.13.5, SciPy 1.17.0, NumPy 2.3.5 and Matplotlib 3.10.8 as applicable. All fixtures are synthetic; ByteMind library execution is unverified.
Plot counts with explicit bin edges
Use a noninteractive Agg backend so the code works in a local headless environment. Bins are left-closed and right-open except the final bin, which includes its right edge.
import matplotlibmatplotlib.use('Agg')import matplotlib.pyplot as pltfig, ax = plt.subplots()counts, edges, _ = ax.hist([1, 2, 2, 3, 4], bins=[1, 2, 3, 4], edgecolor='black')ax.set(xlabel='Reading (units)', ylabel='Count', title='Synthetic readings')fig.canvas.draw()print(counts.astype(int).tolist())print(edges.astype(int).tolist())plt.close(fig)Expected output:
[1, 2, 2][1, 2, 3, 4]Text equivalent: [1,2) contains one observation; [2,3) contains two; [3,4] contains two. Counts sum to five. The figure is rendered in memory; no file is written.
Show signed residuals and measurement bounds
Compare observations against fixed predictions. Residual is observed minus predicted. The vertical bars here represent a hypothetical known measurement bound of 0.5 units, not a confidence interval.
import matplotlibmatplotlib.use('Agg')import matplotlib.pyplot as pltpred = [2, 4, 6]obs = [3, 3, 6]r = [y-p for y,p in zip(obs, pred)]fig, ax = plt.subplots()ax.scatter(pred, r, marker='o', label='Residual')ax.errorbar(pred, r, yerr=0.5, fmt='none', capsize=4, label='Measurement bound')ax.axhline(0, linestyle='--', color='black')ax.set(xlabel='Prediction (units)', ylabel='Residual (units)', title='Residual diagnostic')ax.legend()fig.canvas.draw()print(list(zip(pred, r)))plt.close(fig)Expected output:
[(2, 1), (4, -1), (6, 0)]Text equivalent: predicted 2 has error +1, predicted 4 has error -1, predicted 6 has error 0; each bound extends 0.5 units above and below its error. Positive residual means underprediction. Three points are insufficient to establish a systematic pattern.
Common mistakes
Do not truncate a count-axis baseline to exaggerate differences or use log axes for signed residuals without an appropriate transformation. Do not connect unordered observations as a time series. Error bars require a defined statistic, units and assumptions; they are not automatically confidence intervals. Add direct labels or marker shapes and a data table rather than relying on color alone.
Practice and next steps
Sketch a scatter plot with prediction on the horizontal axis. Add a zero reference line and describe each error in words. Complete the chapter milestone with a data table alongside the figures. The separate pure-Python exercise assesses the underlying calculation, not library execution.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Accessible Matplotlib plots — knowledge check
Apply scientific computing and visualization reasoning to synthetic examples.
Which object represents the complete Matplotlib canvas?
Bins [0,1,2] and values [0,1,2] yield what counts?
Observation 7, prediction 9: signed residual?
How should you describe error bars?
What helps a learner unable to see a plot?