0Pricing
Python Academy · Lesson

Figures and Axes

Understand the plotting model.

Figures and Axes is a free Python Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Plotting Model

Matplotlib is the foundational plotting library for Python. To use it well you must understand its two-level model: the Figure and the Axes.

A Figure is the overall window or page that everything is drawn on. An Axes is a single plot inside that figure, with its own x and y coordinate system.

  • One figure can hold many axes
  • Each axes is where the data actually lives

Figure vs Axes vs Axis

Three terms are easy to confuse:

  • Figure the whole image, the container
  • Axes a single plotting region (note the spelling, it is not the plural of axis)
  • Axis the x-axis or y-axis number line inside an axes

So a figure contains axes, and each axes contains two (or three) axis objects.

Creating a Figure and Axes

The recommended way to start is plt.subplots(). It returns a figure and an axes in one call.

The model itself is simple to reason about even without rendering. Here we simulate what the objects represent.

fig = {'type': 'Figure', 'axes': []}
ax = {'type': 'Axes', 'parent': 'Figure'}
fig['axes'].append(ax)
print('Figure holds', len(fig['axes']), 'axes')
print('Axes parent is the', ax['parent'])

The pyplot Interface

Matplotlib offers two styles. The pyplot (state-based) style uses plt.plot() and tracks the current figure automatically. The object-oriented style uses ax.plot() explicitly.

For anything beyond a quick sketch, prefer the object-oriented style. It is clearer when you have multiple axes.

Why Object-Oriented Wins

When you call plt.title('X'), it applies to whatever the current axes happens to be. With multiple subplots this becomes ambiguous.

With ax.set_title('X') there is no guessing: the method targets exactly the axes you named.

Coordinate Systems

Each axes maps data values to display positions. Data coordinates are what your numbers mean; the axes scales them to fit the drawing area.

We can model that mapping with plain math.

data_min, data_max = 0, 100
pixel_min, pixel_max = 0, 400

def to_pixels(value):
    frac = (value - data_min) / (data_max - data_min)
    return pixel_min + frac * (pixel_max - pixel_min)

for v in [0, 25, 50, 100]:
    print('data', v, '->', to_pixels(v), 'px')

Figure Size and DPI

A figure has a size in inches and a resolution in dots per inch (DPI). The pixel size is the product of the two.

For example a 6x4 inch figure at 100 DPI is 600x400 pixels.

width_in, height_in = 6, 4
dpi = 100
print('Pixels:', width_in * dpi, 'x', height_in * dpi)

Adding Axes Manually

Besides plt.subplots(), you can add an axes at a precise rectangle with fig.add_axes([left, bottom, width, height]), where each value is a fraction of the figure between 0 and 1.

This is how inset plots and custom layouts are built.

The Current Figure

Pyplot keeps a registry of figures and a pointer to the current one. plt.gcf() gets the current figure and plt.gca() gets the current axes.

These exist mainly to support the state-based style. In object-oriented code you hold your own references instead.

Showing and Closing

plt.show() renders all open figures in a window. plt.close() frees a figure. In scripts that produce many figures, closing them prevents memory buildup.

In notebooks the figure is displayed inline automatically.

A Mental Checklist

Before drawing anything, ask:

  • How many figures do I need?
  • How many axes per figure?
  • Which axes does each piece of data belong to?

Answering these first makes the object-oriented API feel natural.

Quick Check

Test your understanding of the figure and axes model.

Recap

You learned the core Matplotlib model:

  • Figure the container, sized in inches times DPI
  • Axes an individual plot with its own coordinates
  • Axis the x or y number line
  • Prefer the object-oriented style with fig, ax = plt.subplots()

With this mental model, every later customization becomes predictable.

Frequently asked questions

Is the “Figures and Axes” lesson free?

Yes — the full text of “Figures and Axes” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.

What will I learn in “Figures and Axes”?

Understand the plotting model. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Figures and Axes” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Figures and Axes
  2. Line, Bar and Scatter Plots
  3. Customizing Plots
  4. Subplots and Layouts
← Back to Python Academy