Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Exploring and Plotting Data  ›  Capstone Project

Histograms, Box Plots and Saving Figures

Histogram 15 minA histogram shows the shape of one numeric column, a box plot summarises median, quartiles and outliers so groups can be compared, and savefig exports the figure.

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 owner's slideStep 1 of 4
  1. The overall shape

    A histogram of copies shows 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.

quick drill

In a box plot, what does the line inside the box mark?

quick drill

A dot sits far above one box's top whisker. What does it mean?

quick drill

Which chart best shows how 5,000 order sizes are distributed?

quick drill

What does bbox_inches="tight" do in savefig?

quick drill

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.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainOne slide, two charts

You must show a manager how delivery times are distributed and whether any of four warehouses is slower or more erratic. Explain which charts you would draw, what each shows, and how you would export them.