In this tutorial, we’ll try to understand how to set the aspect ratio of a plot in Matplotlib with the help of some examples.

Before looking at how to set an aspect ratio, let us first understand what is the aspect ratio.

**In matplotlib, the Aspect ratio is simple, the ratio of length to width of any plot or image that we want to display.**

## Setting Aspect ratio

You can easily adjust the aspect ratio in matplotlib by using the `set_aspect`

method from the axes class. Since this method is an `Axes`

class method, you’ll have to use the plot’s respective Axes object to use this.

**Basic Syntax:**

Axes.set_aspect(aspect, adjustable=None, anchor=None, share=False)

**Parameters:**

**aspect:**{‘auto’, ‘equal’} or float**auto**: fill the position rectangle with data (this is the default).**equal**: same as`aspect=1`

, i.e. same scaling for x and y.**float**: The displayed size of 1 unit in y-data coordinates will be aspect times the displayed size of 1 unit in x-data coordinates; e.g. for aspect=2 a square in data coordinates will be rendered with a height of twice its width.

**adjustable:**None or {‘box’, ‘datalim’}, optional- If not None, this defines which parameter will be adjusted to meet the required aspect. See set_adjustable for further details.

For a detailed explanation of the remaining parameters, refer to this.

## Examples

Now, let us see some examples to demonstrate the above method.

### Example 1 – When the aspect ratio is not defined

**Code:**

import matplotlib.pyplot as plt import numpy as np x = np.arange(0,5,0.01) y = np.sin(10*x) plt.figure(figsize = (10,3)) plt.plot(x,y)

Output:

In the above code, we first imported the required modules. Then we generated a range of x values using `np.arange`

method(more details here ). Then, we generated the corresponding y values considering sin10x as our curve, which we plot. Then finally, we’re adjusting the figure size and plotting the curve.

### Example 2 – Adjusting the aspect ratio

Use the Axes object’s `set_aspect()`

function to set the aspect ratio of a plot. Pass the desired aspect ratio (height-to-width ratio) as an argument.

**Code:**

import matplotlib.pyplot as plt import numpy as np x = np.arange(0,5,0.01) y = np.sin(10*x) plt.figure(figsize = (10,3)) ax = plt.gca() #you first need to get the axis handle ax.set_aspect(2) #sets the height to weight ratio to 2 plt.plot(x,y)

Output:

In the above code, we find imported required modules. Then we generated a range of x values using `np.arange`

method and generated the corresponding y values considering sin10x as our curve, which we plot. Then, we’re adjusting the figure and set the aspect ratio to be equal to 2. Note that we use the `plt.gca()`

function to get the `Axes`

object of the current plot using which we apply the `set_aspect()`

function.

### Example 3 – Adjusting the aspect ratio to be ‘equal’

Let’s now set the aspect ratio to be ‘equal’ which sets the aspect ratio to 1.

**Code:**

import matplotlib.pyplot as plt import numpy as np x = np.arange(0,5,0.01) y = np.sin(10*x) plt.figure(figsize = (10,3)) ax = plt.gca() #you first need to get the axis handle ax.set_aspect('equal') plt.plot(x,y)

Output:

Here, we’re adjusting the figure and setting the aspect ratio to ‘equal’ (which indicates value 1), and plotting the figure.

You might also be interested in –

- Change Line Thickness in Matplotlib
- How to remove the legend border (frame) in Matplotlib?
- How to change the legend position in Matplotlib?
- Matplotlib – Change Line to Dots
- Matplotlib – Add an Average Line to the Plot

**Subscribe to our newsletter for more informative guides and tutorials. ****We do not spam and you can opt out any time.**