Box plots make several numeric distributions comparable in one compact figure. In Matplotlib, a box plot shows each group's median, quartile range, whiskers, and outlier points, which helps when values need more context than a single average.
Use Axes.boxplot() with one numeric sequence for each box. The sequences stay in the order supplied to the function, so the category labels must follow the same order as the data.
Current Matplotlib releases use tick_labels for the box labels. Use patch_artist=True when the boxes need fill colors, because filled boxes are returned as patch artists that can be styled after plotting.
Related: How to create a histogram in Matplotlib
Related: How to set axis labels in Matplotlib
from pathlib import Path import matplotlib.pyplot as plt import numpy as np rng = np.random.default_rng(42) latency_ms = [ rng.normal(95, 12, 80), rng.normal(110, 18, 80), rng.normal(132, 25, 80), ] labels = ["API", "Batch", "Search"] colors = ["#d8ecff", "#e6f4d7", "#ffe2c2"] fig, ax = plt.subplots(figsize=(7, 4), layout="constrained") result = ax.boxplot( latency_ms, tick_labels=labels, patch_artist=True, medianprops={"color": "black", "linewidth": 1.4}, boxprops={"edgecolor": "#3973ac"}, ) for patch, color in zip(result["boxes"], colors): patch.set_facecolor(color) ax.set_ylabel("Latency (ms)") ax.set_title("Service latency by workload") ax.grid(axis="y", linestyle=":", alpha=0.5) output = Path("box-plot-create.png") fig.savefig(output, dpi=150) print(f"boxes: {len(result['boxes'])}") print(f"medians: {len(result['medians'])}") print(f"saved: {output}")
Keep one array in latency_ms for each box and one matching string in labels. The tick_labels argument replaced the older labels name in current Matplotlib releases. The boxes list contains one artist per plotted group when patch_artist=True is used.
$ python box_plot_create.py boxes: 3 medians: 3 saved: box-plot-create.png
The figure should show three labeled boxes, median lines inside each box, whiskers extending from the boxes, and outlier markers where values fall beyond the whiskers. If the x-axis shows numeric ticks instead of labels, confirm that tick_labels has one label for each data array.