Module 6 · Matplotlib: Charts That Explain · Lesson 20 of 51

Histograms, scatter plots, box plots and grids

Show spread with histograms and box plots, relationships with scatter plots, several charts at once with subplots, and quick charts straight from pandas.

In this lesson · about 40 minutes

  • Draw a histogram and choose sensible bins
  • Draw a scatter plot and read a relationship from it
  • Build a grid of charts and use pandas' .plot shortcut

The code

ax.hist(orders["total"], bins=20, edgecolor="white")
ax.scatter(daily["temp_c"], daily["sales"], alpha=0.5, s=15)
ax.boxplot([group1, group2], labels=["Card", "Cash"])   # tick_labels= in newer versions
  • Bins: too few hide the shape, too many make noise. Start with 15–30 and adjust.
  • alpha (transparency) helps when dots overlap; s sets dot size; c= colours dots by a third variable.
  • Several charts: fig, axes = plt.subplots(2, 2, figsize=(10, 7)) gives a 2×2 grid; draw on axes[0, 1] and so on. sharey=True puts them on the same scale so they're comparable.
  • pandas shortcut: orders["total"].plot.hist(bins=20), monthly.plot(marker="o"), df.plot.scatter(x="temp_c", y="sales"). These use Matplotlib underneath and return an ax you can keep customising.

Reading a scatter plot

Look for direction (up, down, none), form (straight or curved), strength (tight or loose) and outliers. Different groups (branches) often hide inside one cloud: colour by group to see them.

Four views of the café in one figure

Exercise

Histogram of member spend

Draw a histogram of members["avg_spend"] on ax with exactly 20 bins, an x-axis label Average spend ($), and the title How much members spend per visit.

Code that runs before yours
import pandas as pd
import matplotlib.pyplot as plt
members = pd.read_csv("kc_members.csv")

Exercise

Scatter: rain and sales

Make a scatter plot of daily["rain_mm"] (x) against daily["sales"] (y) on ax with alpha=0.4, and label the axes Rain (mm) and Sales ($).

Code that runs before yours
import pandas as pd
import matplotlib.pyplot as plt
daily = pd.read_csv("kc_daily.csv", parse_dates=["date"])

Check your understanding0 of 3 correct

Question 1

1.A histogram with 3 bins looks like a block. What should you try?

Question 2

2.In a scatter plot, the dots form a loose upward cloud. This suggests…

Question 3

3.Why use sharey=True on side-by-side charts?

Quiz score: 0 of 3

Exercises solved: 0 of 2

Tip: answer every quiz question correctly first.

Written by Noviq Academy in its own words. Kettle & Crumb and all its data are made up for teaching; any resemblance to a real business is a coincidence. Python runs in your browser with Pyodide, so results can differ slightly from your own computer. Power BI, Tableau and other product names belong to their owners. Read-aloud uses your device's built-in voices.