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.
The shortcut
Ask "one column or a comparison?" One column's full shape → histogram. The same measure across groups → box plots side by side. Finish every chart the same way: labels, then savefig, then show.
The overall shape
A histogram of
copiesshows most sales at 2 or 3 copies, none above 6.
The trap is trusting one histogram's bin choice. The same data with 3 bins and with 30 bins can tell different stories; try a couple before you conclude.
Drills
Answer fast — trust the rule.
In a box plot, what does the line inside the box mark?
A dot sits far above one box's top whisker. What does it mean?
Which chart best shows how 5,000 order sizes are distributed?
What does bbox_inches="tight" do in savefig?
Sales per branch: Chennai median 5, Delhi 3, Pune 2. Which call draws them side by side?
Remember
- Histograms show shape; box plots show median, middle half and outliers.
- One column → histogram; a measure across groups → box plots side by side.
- Label, then
fig.savefig(..., bbox_inches="tight"), then show.
