Set Axis Range (axis limits) in Matplotlib Plots

The matplotlib library in Python comes with a number of useful functions and customizations that help you modify your plot to a great extent. In this tutorial, we will look at how to set the axis range in a matplotlib plot with the help of some examples.

Set Axis Limits using `xlim()` and `ylim()`

You can use the `maplotlib.pyplot`‘s `xlim()` and `ylim()` functions to set the axis ranges for the x-axis and the y-axis respectively. Pass the axis range (axis limits) as a tuple to these functions.

The following is the syntax –

```import matplotlib.pyplot as plt

# create a plot - for example, a scatter plot
plt.scatter(x, y)

# set the x-axis limit to (x1, x2)
plt.xlim((x1, x2))

# set the y-axis limit to (y1, y2)
plt.ylim((y1, y2))```

Let’s now look at some examples of using the above syntax. For this, we will be using the US dollar to Indian rupee conversion data from 2011 to 2020. Let’s plot the data as a scatter plot.

```import matplotlib.pyplot as plt

# x values - years
x = [2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020]
# y values - 1 USD in INR
y = [46.67, 53.44, 56.57, 62.33, 62.97, 66.46, 67.79, 70.09, 70.39, 76.38]

# plot x and y on scatter plot
plt.scatter(x, y)

# add axes labels
plt.xlabel('Year')
plt.ylabel('1USD in INR')```

Output:

Example 1 – Set the x-axis range with `xlim()`

Let’s say you only want to see the data for 2014 to 2020. For this, you can set the x-axis limit using `xlim()` function on the matplotlib pyplot object `plt`.

```# plot x and y on scatter plot
plt.scatter(x, y)

# add axes labels
plt.xlabel('Year')
plt.ylabel('1USD in INR')

# set the x-axis range
plt.xlim((2014, 2020))```

Output:

Example 2 – Set the y-axis range with `ylim()`

You can similarly modify the y-axis limit using the `ylim()` function on the matplotlib pyplot object `plt`.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

Restricting or modifying the y-axis range is not as common as changing the x-axis range but depending upon the use case you may need to do it. In the above example, let’s change the y-axis range to 50 to 65.

```# plot x and y on scatter plot
plt.scatter(x, y)

# add axes labels
plt.xlabel('Year')
plt.ylabel('1USD in INR')

# set the y-axis range
plt.ylim((50, 65))```

Output:

Note that if you’re working with multiple subplots you can use the `set_xlim()` and `set_ylim()` functions on the individual `axes` objects (for example `ax1`) to change the axis limits for each subplot.

You might also be interested in –

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

Author

• Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top