Charts with matplotlib · lesson 2 of 11
Parts in this lesson
Afig, ax = plt.subplots()a figure holding one chart
Bax.plot(x, y, label="rice")a line on that chart
Cax.set_title(...), ax.set_xlabel(...)the words round it
Dplt.show()draw the figure now

Figures and axes

Last updated

matplotlib has two ways to draw: quick commands that act on "the current chart", and objects you hold and change by name. This course uses the objects. In this lesson you'll draw a chart line by line, learn the name of every part of it, compare the two ways, and see what happens when you mix them in a figure with two charts.

By the end of this lesson you can

  • Make a figure and axes with plt.subplots and draw on the axes
  • Name the parts of a chart: figure, axes, spines, ticks, labels, title, legend
  • Tell the pyplot way from the object-oriented way, and avoid mixing them

Your first chart, line by line

The first cell loads the clean Tindahan sales from Stage 5 and keeps the Batangas store's first two weeks of January, one column per item (pivot, lesson 4.4). Run all three cells.

charts.ipynb

Line by line, the second cell:

  • fig, ax = plt.subplots(figsize=(7, 3.5)) makes a figure, 7 inches wide and 3.5 high, with one axes in it: one chart. You get both back, and every later line uses ax. (plt is matplotlib.pyplot, imported in the first cell.)
  • ax.plot(x, y, label="rice") draws a line through the points. x is the day of the month, daily.index.day; y is the rice column. A second ax.plot adds a second line; linestyle="--" makes it dashed so it doesn't rely on colour.
  • ax.set_title, ax.set_xlabel, ax.set_ylabel add the words. ax.set_ylim(0, 75) starts the y-axis at zero.
  • ax.legend() makes a key from each line's label.
  • plt.show() draws the figure. In Jupyter, a figure is drawn at the end of the cell anyway; plt.show() says so on purpose and also stops the notebook printing the value of the cell's last line.

The third cell checks what you made: a Figure, an Axes, two lines on it, and the title. Change the title, or the figsize, and run the cell again.

The anatomy of a figure

matplotlib's own documentation has a famous picture naming every part of a chart. Here is the same idea on your chart. Step through it.

The parts of a matplotlib figure. A finished chart. Every part of it has a name in matplotlib, and an object you can change.14710130204060Batangas store, Januaryday of the monthkilos soldricesugar
1 of 8 A finished chart. Every part of it has a name in matplotlib, and an object you can change.

The two words that matter most:

  • A Figure is the whole image. It can hold one chart or twenty.
  • An Axes (always with an s, even for one) is one chart: a plotting area with its own x-axis, y-axis, title and data. Don't confuse it with an axis, which is just one of its two scales.

Nearly every change you'll make in this stage is a method on ax: ax.set_… to change a part, ax.plot, ax.bar and friends to add data.

Two ways to draw

matplotlib grew up copying MATLAB, where you don't hold any objects: commands like plt.plot and plt.title act on "the current chart", normally the last one made. That's the pyplot way. The object-oriented way, which you've been using, names the chart and calls methods on it. Run both cells.

two-ways.ipynb

They draw the same chart. Most plt. commands have an ax. twin, though not always with the same name:

pyplotobject-oriented
plt.figure()fig, ax = plt.subplots()
plt.plot(x, y)ax.plot(x, y)
plt.title("…")ax.set_title("…")
plt.xlabel("…"), plt.ylabel("…")ax.set_xlabel("…"), ax.set_ylabel("…")
plt.xlim(0, 10)ax.set_xlim(0, 10)
plt.legend()ax.legend()

The pyplot way is fine for a quick look at one chart. As soon as a figure has two charts, "the current chart" becomes a guess, so this course always uses the object-oriented way, and so does matplotlib's own advice for anything more than a quick plot. You'll still meet plt. code online; the table turns it into ax. code.

Wrong way: the title on the wrong chart

A figure with two charts: rice on the left, sugar on the right. The title and the y-axis label were written the pyplot way. Run the first cell.

wrong.ipynb

"Rice" sits above the sugar chart, and "kilos" labels the right-hand axis only. No error: plt.title titled the current axes, and that's the last one made. plt.subplots(1, 2) made left, then right, so right is current. Drawing with left.plot doesn't change that: only pyplot commands do.

Someone reading the figure now believes the right-hand line is rice. It's the kind of mistake that survives into a report, because nothing looks broken.

The second cell names each chart: left.set_title("Rice"), right.set_title("Sugar"). Each label goes exactly where it says. The check: in a figure with more than one chart, every line that changes a chart should start with that chart's name.

Check what you learned

In fig, ax = plt.subplots(), what is ax?
Predict, then run it: fig, (a, b) = plt.subplots(1, 2), then a.plot([1, 2]), b.plot([2, 1]), plt.title("Up"). Which chart gets the title?
What is the object-oriented twin of plt.xlabel("day")?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Data Analysis