# Set the Aspect Ratio and Equal Axes in Matplotlib

> Set equal axes in Matplotlib with ax.set_aspect("equal") or ax.axis("equal"), make a square plot with ax.set_box_aspect(1), and fix common aspect ratio errors.

Source: https://www.pythoncharts.com/matplotlib/matplotlib-aspect-ratio/ | Published: 2026-11-27 | Author: Alex Scott | Category: Matplotlib | Tags: matplotlib, aspect ratio, axis limits, layout

To make one unit on the x-axis the same length as one unit on the y-axis in Matplotlib, call `ax.set_aspect("equal")`. To make the plot itself square (or any other shape) without tying it to the data, call `ax.set_box_aspect(1)`.

## Quick Example

A circle drawn with the default aspect looks like an ellipse. With `"equal"`, it looks like a circle:

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

theta = np.linspace(0, 2 * np.pi, 200)
x, y = np.cos(theta), np.sin(theta)

fig, axs = plt.subplots(1, 2, figsize=(9, 3.6), layout="constrained")
for ax, aspect in zip(axs, ["auto", "equal"]):
    ax.plot(x, y, color="#6a4c93", linewidth=3)
    ax.set_aspect(aspect)
    ax.set_title(f'set_aspect("{aspect}")')
plt.show()
```

![The same circle drawn twice: stretched into a wide ellipse with the auto aspect, and perfectly round with the equal aspect](https://www.pythoncharts.com/matplotlib/matplotlib-aspect-ratio/images/aspect-ratio-quick-example.png)

`"auto"` is the default for line and scatter plots: the plot fills the space available, whatever that does to the scales.

## What set_aspect() Values Mean

The number you pass is the height of one y unit divided by the width of one x unit. `"equal"` is the same as `1`. With `2`, a step of 1 on the y-axis is drawn twice as long as a step of 1 on the x-axis.

```python
fig, axs = plt.subplots(1, 3, figsize=(9, 3.6), layout="constrained")
for ax, aspect in zip(axs, [0.5, 1, 2]):
    ax.plot([0, 1, 1, 0, 0], [0, 0, 1, 1, 0], color="#1982c4", linewidth=3)
    ax.set_aspect(aspect)
    ax.set_title(f"set_aspect({aspect})")
plt.show()
```

![A one-by-one square drawn with aspect values 0.5, 1 and 2, appearing wide, square and tall](https://www.pythoncharts.com/matplotlib/matplotlib-aspect-ratio/images/aspect-ratio-values.png)

## Equal Axes Without Shrinking the Plot

By default Matplotlib keeps your data limits and changes the shape of the plot, which can leave a lot of empty figure. Pass `adjustable="datalim"` to keep the plot's shape and widen one of the axis ranges. `ax.axis("equal")` does the same thing.

```python
rng = np.random.default_rng(3)
east = rng.normal(0, 4.0, 120)
north = 0.3 * east + rng.normal(0, 0.8, 120)

fig, axs = plt.subplots(1, 2, figsize=(9, 3.8), layout="constrained")
for ax, adjustable in zip(axs, ["box", "datalim"]):
    ax.scatter(east, north, s=18, color="#ff595e")
    ax.set_aspect("equal", adjustable=adjustable)
    ax.set_title(f'adjustable="{adjustable}"')
    ax.set_xlabel("East (km)")
    ax.set_ylabel("North (km)")
plt.show()
```

![Equal-aspect scatter plot of positions: with adjustable box the plot becomes a short wide strip, with adjustable datalim it keeps its height and the y range extends further](https://www.pythoncharts.com/matplotlib/matplotlib-aspect-ratio/images/aspect-ratio-adjustable.png)

Both panels have the same scale in each direction. The left one is exactly as tall as the data needs; the right one shows extra empty range above and below the points.

Use `"box"` when your axis limits matter, and `"datalim"` when the layout matters. With `"datalim"`, limits you set with `set_xlim()` or `set_ylim()` may be widened to satisfy the ratio.

## Make a Square Plot With set_box_aspect()

`ax.set_box_aspect()` sets the height of the plot area divided by its width. It ignores the data units, so x and y can cover completely different ranges.

```python
hours = np.linspace(0, 12, 100)
charge = 100 * (1 - np.exp(-hours / 3))

fig, axs = plt.subplots(1, 2, figsize=(9, 3.8), layout="constrained")
for ax, ratio, title in zip(axs, [1, 0.5], ["Square", "Half as tall as wide"]):
    ax.plot(hours, charge, color="#8ac926", linewidth=3)
    ax.set_box_aspect(ratio)
    ax.set_title(title)
    ax.set_xlabel("Hours on charger")
    ax.set_ylabel("Charge (%)")
plt.show()
```

![A battery charging curve in a square plot and in a plot half as tall as it is wide](https://www.pythoncharts.com/matplotlib/matplotlib-aspect-ratio/images/aspect-ratio-box-aspect.png)

This is the one to use for ROC curves, predicted-vs-actual plots, or a grid of panels that should all have the same shape.

## set_aspect() vs set_box_aspect() vs figsize

| You want | Use |
|---|---|
| Circles to look round, distances to be comparable | `ax.set_aspect("equal")` |
| The same, without changing the plot's shape | `ax.axis("equal")` |
| A square or fixed-shape plot with any data | `ax.set_box_aspect(1)` |
| A wider or taller image file | `plt.subplots(figsize=(10, 4))` |

`figsize` sets the size of the whole figure, including labels and titles, so `figsize=(5, 5)` doesn't give an exactly square plot area. See [how to change the figure size](https://www.pythoncharts.com/matplotlib/figure-size-matplotlib/).

## Images and imshow()

`ax.imshow()` uses an equal aspect by default, so pixels are square. Pass `aspect="auto"` to stretch the array to fill the plot, which is usually what you want for a matrix with many more columns than rows.

```python
signal = rng.random((12, 120))

fig, axs = plt.subplots(2, 1, figsize=(8, 4), layout="constrained")
for ax, aspect in zip(axs, ["equal", "auto"]):
    ax.imshow(signal, aspect=aspect, cmap="magma")
    ax.set_title(f'imshow(aspect="{aspect}")')
plt.show()
```

![A 12 by 120 array shown as a thin strip with square pixels and as a taller block that fills the width with auto aspect](https://www.pythoncharts.com/matplotlib/matplotlib-aspect-ratio/images/aspect-ratio-imshow.png)

More on array plotting in [displaying images with imshow()](https://www.pythoncharts.com/matplotlib/matplotlib-imshow-display-image/).

## Fix: "not allowed when both axes are shared"

`ax.axis("equal")` or `adjustable="datalim"` on subplots created with both `sharex=True` and `sharey=True` raises this when the figure is drawn:

```text
RuntimeError: set_aspect(..., adjustable='datalim') or axis('equal') are not allowed when both axes are shared.  Try set_aspect(..., adjustable='box').
```

Call `ax.set_aspect("equal")` instead, which uses `adjustable="box"`. See [shared axes](https://www.pythoncharts.com/matplotlib/matplotlib-shared-axes-subplots/) for how linked limits work.

## Similar Topics

- [Set Axis Limits in Matplotlib](https://www.pythoncharts.com/matplotlib/matplotlib-axis-limits-xlim-ylim/)
- [How to Change the Figure Size in Matplotlib](https://www.pythoncharts.com/matplotlib/figure-size-matplotlib/)
- [Contour and Filled Contour Plots](https://www.pythoncharts.com/matplotlib/matplotlib-contour-plot/)
