# Matplotlib Figure vs Axes: pyplot and the Object-Oriented Interface

> Matplotlib Figure vs Axes vs pyplot: use fig, ax = plt.subplots() and call methods on ax. Includes a plt-to-ax translation table and the current-axes pitfall.

Source: https://www.pythoncharts.com/matplotlib/matplotlib-figure-vs-axes/ | Published: 2026-11-27 | Author: Alex Scott | Category: Matplotlib | Tags: matplotlib, figure, axes, pyplot, object-oriented, subplots

In Matplotlib, the **Figure** is the whole image and an **Axes** is one plot inside it, with its own data, ticks, and title. The object-oriented interface creates both with `fig, ax = plt.subplots()` and calls methods on them, while pyplot functions such as `plt.plot()` draw on whichever Axes is "current".

## Quick Example

One Figure holding two Axes, each changed through its own variable:

```python
import matplotlib.pyplot as plt
import numpy as np

months = np.array(["Jan", "Feb", "Mar", "Apr", "May", "Jun"])
rain = np.array([142, 108, 96, 63, 47, 31])
temp = np.array([5.8, 7.1, 9.4, 12.0, 15.3, 18.1])

fig, (ax_rain, ax_temp) = plt.subplots(1, 2, figsize=(9, 3.8), layout="constrained")

ax_rain.bar(months, rain, color="#4338ca")
ax_rain.set_title("Rainfall")
ax_rain.set_ylabel("mm")

ax_temp.plot(months, temp, "o-", color="#ea580c")
ax_temp.set_title("Average temperature")
ax_temp.set_ylabel("°C")

fig.suptitle("First half of the year")
plt.show()
```

![Matplotlib figure with a rainfall bar chart on the left Axes and a temperature line chart on the right Axes under one figure title](https://www.pythoncharts.com/matplotlib/matplotlib-figure-vs-axes/images/matplotlib-figure-vs-axes-quick-example.png)

## Figure, Axes, and Axis: What Is the Difference?

| Object | What it is | Typical methods |
|---|---|---|
| `Figure` | The whole canvas, which is what gets saved to a file | `fig.suptitle()`, `fig.savefig()`, `fig.legend()`, `fig.colorbar()` |
| `Axes` | One plotting area with its data, title, and legend | `ax.plot()`, `ax.bar()`, `ax.set_title()`, `ax.legend()` |
| `Axis` | The x or y ruler of an Axes: ticks, tick labels, and scale | `ax.xaxis.set_major_locator()`, `ax.yaxis.set_major_formatter()` |

Despite how it sounds, "an Axes" is a single plot object. A 2D Axes owns two Axis objects, `ax.xaxis` and `ax.yaxis`, and a Figure can own any number of Axes (listed in `fig.axes`).

![Diagram of a Matplotlib Figure containing two labeled Axes, with arrows pointing to the x Axis and y Axis of the left one](https://www.pythoncharts.com/matplotlib/matplotlib-figure-vs-axes/images/matplotlib-figure-vs-axes-anatomy.png)

## pyplot vs the Object-Oriented Interface

Both interfaces draw the same objects. pyplot keeps track of a current Figure and current Axes for you:

```python
plt.figure(figsize=(7, 3.5))
plt.plot(months, temp, "o-", color="#ea580c")
plt.title("Average temperature")
plt.ylabel("°C")
plt.show()
```

The object-oriented version names those objects and calls methods on them:

```python
fig, ax = plt.subplots(figsize=(7, 3.5))
ax.plot(months, temp, "o-", color="#ea580c")
ax.set_title("Average temperature")
ax.set_ylabel("°C")
plt.show()
```

![Line chart of average temperature rising from January to June](https://www.pythoncharts.com/matplotlib/matplotlib-figure-vs-axes/images/matplotlib-figure-vs-axes-line.png)

You still use pyplot in the second version, to create the figure (`plt.subplots()`) and display it (`plt.show()`). The difference is which object receives the drawing commands.

## How to Translate plt Commands to ax Methods

Most pyplot functions have an `ax` method with the same name. The labeling and limit functions gain a `set_` prefix.

| pyplot | Object-oriented |
|---|---|
| `plt.plot()`, `plt.scatter()`, `plt.bar()`, `plt.hist()` | `ax.plot()`, `ax.scatter()`, `ax.bar()`, `ax.hist()` |
| `plt.title()` | `ax.set_title()` |
| `plt.xlabel()`, `plt.ylabel()` | `ax.set_xlabel()`, `ax.set_ylabel()` |
| `plt.xlim()`, `plt.ylim()` | `ax.set_xlim()`, `ax.set_ylim()` |
| `plt.xscale()`, `plt.yscale()` | `ax.set_xscale()`, `ax.set_yscale()` |
| `plt.xticks(positions, labels)` | `ax.set_xticks(positions, labels)` |
| `plt.xticks(rotation=45)` | `ax.tick_params(axis="x", labelrotation=45)` |
| `plt.legend()`, `plt.grid()` | `ax.legend()`, `ax.grid()` |
| `plt.axhline()`, `plt.text()`, `plt.annotate()` | `ax.axhline()`, `ax.text()`, `ax.annotate()` |
| `plt.figure(figsize=(8, 4))` | `fig, ax = plt.subplots(figsize=(8, 4))` |
| `plt.colorbar()` | `fig.colorbar(mappable, ax=ax)` |
| `plt.suptitle()`, `plt.savefig()`, `plt.tight_layout()` | `fig.suptitle()`, `fig.savefig()`, `fig.tight_layout()` |

`ax.set()` sets several properties in one call: `ax.set(title="Rainfall", ylabel="mm", ylim=(0, 160))`.

## Why plt.title() Changes the Wrong Subplot

After `plt.subplots(1, 2)`, the current Axes is the last one created, so a pyplot call lands on the right-hand panel no matter which one you drew on last.

```python
fig, (left, right) = plt.subplots(1, 2, figsize=(9, 3.2), layout="constrained")
left.bar(months, rain, color="#4338ca")
plt.title("Rainfall (mm)")  # goes on the right-hand Axes

print(repr(left.get_title()))
print(repr(right.get_title()))
plt.show()
```

This prints `''` and then `'Rainfall (mm)'`:

![Two subplots where the rainfall title sits above the empty right panel instead of the bar chart on the left](https://www.pythoncharts.com/matplotlib/matplotlib-figure-vs-axes/images/matplotlib-figure-vs-axes-wrong-subplot.png)

Use `left.set_title(...)` instead. `plt.gca()` returns the current Axes and `plt.gcf()` the current Figure, which helps when you inherit pyplot code, and `plt.sca(left)` changes which Axes is current.

## Write Plotting Functions That Accept an Axes

A function that takes `ax` can draw a standalone chart or fill one panel of a larger layout. This is the main practical reason to prefer the object-oriented style.

```python
def plot_monthly(values, title, color, ax=None):
    if ax is None:
        ax = plt.gca()
    ax.bar(months, values, color=color)
    ax.set_title(title)
    ax.spines[["top", "right"]].set_visible(False)
    return ax

sunshine = np.array([58, 84, 132, 178, 219, 246])

fig, axes = plt.subplots(1, 3, figsize=(10, 3.2), layout="constrained")
plot_monthly(rain, "Rain (mm)", "#4338ca", ax=axes[0])
plot_monthly(temp, "Temperature (°C)", "#ea580c", ax=axes[1])
plot_monthly(sunshine, "Sunshine (hours)", "#ca8a04", ax=axes[2])
plt.show()
```

![Three bar charts of rain, temperature, and sunshine hours drawn by the same function into three Axes](https://www.pythoncharts.com/matplotlib/matplotlib-figure-vs-axes/images/matplotlib-figure-vs-axes-function.png)

Pandas and Seaborn follow the same convention. `df.plot(ax=ax)` and axes-level Seaborn functions such as `sns.lineplot(..., ax=ax)` draw into the Axes you pass.

## Create a Figure Without pyplot

In a web server or a GUI app, you can build a `Figure` directly. pyplot doesn't track it, so it needs no `plt.close()` and `plt.show()` won't display it.

```python
from matplotlib.figure import Figure

report = Figure(figsize=(6, 3), layout="constrained")
ax = report.subplots()
ax.plot(months, rain, color="#4338ca")
report.savefig("rain.png", dpi=150)
```

pyplot keeps every figure it creates until you close it. In a loop that saves many charts, call `plt.close(fig)` after each `fig.savefig()`; otherwise Matplotlib warns once more than 20 figures are open.

## When to Use Which Interface

| Situation | Use |
|---|---|
| A quick look at data in a notebook | pyplot is fine |
| Two or more subplots, twin axes, insets | Object-oriented |
| Reusable functions, scripts you'll maintain | Object-oriented |
| Flask, FastAPI, or Django generating images | `matplotlib.figure.Figure` |

Pick one style per script. Mixing `plt.xlabel()` and `ax.set_title()` works until a second Axes appears.

For grids of plots, see [Matplotlib subplots](https://www.pythoncharts.com/matplotlib/matplotlib-subplots/). The [Matplotlib cheat sheet](https://www.pythoncharts.com/matplotlib/matplotlib-cheat-sheet/) uses the object-oriented form throughout.

## Similar Topics

- [Matplotlib Subplots](https://www.pythoncharts.com/matplotlib/matplotlib-subplots/)
- [Changing the Figure and Plot Size in Matplotlib](https://www.pythoncharts.com/matplotlib/figure-size-matplotlib/)
- [How to Save a Figure in Matplotlib with savefig](https://www.pythoncharts.com/matplotlib/matplotlib-savefig/)
- [Seaborn Figure-Level vs Axes-Level Functions](https://www.pythoncharts.com/seaborn/seaborn-figure-level-vs-axes-level/)
