Skip to content
LearnPY
Matplotlib

Understand figures and axes

Build a chart with the object-oriented API, then label the figure so another person can read it without guessing.

10 min readbeginnermatplotlib, numpy

By the end

  • Distinguish a Figure from an Axes.
  • Create a chart with fig, ax = plt.subplots().
  • Add a title and axis labels.
  • Control grid lines and figure size.
  • Inspect the Axes object instead of relying on the picture alone.

Figure versus Axes

A Figure is the whole canvas. An Axes is one plotting area inside that canvas, with its own x-axis, y-axis, data, title, and legend. A figure can contain one axes or many; most first charts use one.

  • Figure: size, layout, and the space that holds axes.
  • Axes: the coordinate system where data is drawn.
  • Axis: the x or y scale, ticks, and labels belonging to an axes.

Your first chart

Pass x values and y values to ax.plot. Matplotlib connects the points in order, so the observation order matters. Here the week number is on the x-axis and study hours are on the y-axis.

Python

A titled, labelled line chart

Ready
Loading editor…

Output

Press Run to execute this cell. The first run downloads Python; later runs reuse the session.

This runner captures open figures after the code finishes, so the chart appears below the output. In a regular Python script you would finish with plt.show().

Labels are part of the chart

A title states the question. Axis labels name the units. Without them a reader has to infer what the numbers mean, and a chart that needs a paragraph of explanation is not finished.

Python

A fixed y range makes comparisons honest

Ready
Loading editor…

Output

Press Run to execute this cell. The first run downloads Python; later runs reuse the session.

Size and layout

figsize is measured in inches before rendering. tight_layout adjusts spacing so labels do not collide with the figure edge. Neither changes the data; they change whether the chart is legible.

Python

A figure sized for its labels

Ready
Loading editor…

Output

Press Run to execute this cell. The first run downloads Python; later runs reuse the session.

The next lesson compares series on one axes and uses bars for categorical totals. The same figure-and-axes pattern stays in place.

Practice

Make it stick

Run the tests as often as you like. Hints and the solution are available when you need them.

Exercise

Create fig and ax with plt.subplots(figsize=(7, 4)). Plot weeks against hours with marker "o", then set the title to "Study hours by week", xlabel to "Week", and ylabel to "Hours".

Your solution

Ready
Loading editor…

Results

Run the tests to check your solution. The first run downloads Python; later runs reuse the session.

Quiz

Check your understanding

01What is the difference between a Figure and an Axes?
02Why prefer fig, ax = plt.subplots() over implicit pyplot calls?
03What should a finished chart include?

Lesson progress