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 get the axis range of a matplotlib plot with the help of some examples.

## Get Axis Limits using `xlim()`

and `ylim()`

You can use the `maplotlib.pyplot`

‘s `xlim()`

and `ylim()`

functions to get the axis ranges for the x-axis and the y-axis respectively. **These functions are used to set the axis range (axis limits) by passing the range as a tuple but if you do not pass anything, it returns the current axis limits.**

The following is the syntax –

import matplotlib.pyplot as plt # create a plot - for example, a scatter plot plt.scatter(x, y) # get the x-axis limits plt.xlim( # get the y-axis limits plt.ylim()

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 – Get the x-axis range with `xlim()`

Let’s get the range of values in the x-axis.

# plot x and y on scatter plot plt.scatter(x, y) # get the x-axis range print(plt.xlim())

Output:

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

You can similarly get the y-axis limits using the `ylim()`

function on the matplotlib pyplot object `plt`

without any arguments.

**Data Science Programs By Skill Level**

**Introductory** ⭐

- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science

**Intermediate ⭐⭐⭐**

- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization

**Advanced ⭐⭐⭐⭐⭐**

- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science

**🔎 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.

# plot x and y on scatter plot plt.scatter(x, y) # get the y-axis range plt.ylim()

Output:

We get the y-axis range as (45.1845, 77.8655).

Note that if you’re working with multiple subplots you can use the `get_xlim()`

and `get_ylim()`

functions on the individual `axes`

objects (for example `ax1`

) to get the axis limits for each subplot.

You might also be interested in –

- Change Font Size of elements in a Matplotlib plot
- Plot a Line Chart in Python with Matplotlib
- Create a Scatter Plot in Python with Matplotlib
- Change Background Color of Plot in Matplotlib
- Add Trendline to a Maplotlib Plot with Code and Output
- How to Add Title to a Plot in Matplotlib? (Code Examples with Output)

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