Histograms, Box Plots and Saving Figures
The owner wants one slide: "What do our sales usually look like, and does any branch behave oddly?" Two charts answer it. A histogram shows the shape of one column; a box plot squeezes centre, spread and outliers into a single picture you can repeat per group. Then the chart has to leave your notebook as a file.
The rule
Histograms show shape; box plots show median, middle half and outliers.
A histogram cuts the number line into bins and draws a bar for how many values fall in each:
fig, ax = plt.subplots()
ax.hist(sales["copies"], bins=[1, 2, 3, 4, 5, 6, 7], edgecolor="white")
ax.set_xlabel("Copies per sale")
ax.set_ylabel("Number of sales")Bars: 1 copy once, 2 twice, 3 twice, then 4, 5 and 6 once each. Each bin includes its left edge and excludes its right, except the last bin, which includes both. Too few bins hide the shape; too many turn it into noise. With a few thousand values, start around 20 to 30.
A box plot reads like this:
- the box runs from Q1 to Q3 (the middle half of the data);
- the line inside is the median;
- the whiskers reach the furthest values still inside the 1.5 × IQR fences;
- dots beyond the whiskers are outliers.
For copies, the box spans 2 to 4.25 with the median at 3, and there are no dots. Box plots shine side by side:
groups = [sales.loc[sales["branch"] == b, "copies"] for b in ["Chennai", "Delhi", "Pune"]]
fig, ax = plt.subplots()
ax.boxplot(groups)
ax.set_xticks([1, 2, 3], ["Chennai", "Delhi", "Pune"])
ax.set_ylabel("Copies per sale")Saving turns the Figure into a file:
fig.savefig("copies_by_branch.png", dpi=200, bbox_inches="tight")dpi sets sharpness, and bbox_inches="tight" trims empty margins so labels are not cut off. Use .png for slides, .svg or .pdf for print. Save before plt.show(): in a script, showing can leave you with a blank figure to save.