Table of Contents
Matplotlib is a powerful data visualization library in Python that allows users to create high-quality graphs and charts. One of the key features of Matplotlib is the ability to set the axis ranges, which refers to the minimum and maximum values displayed on the x and y axis. This allows for customization of the graph to focus on specific data points or to provide a more comprehensive view of the data. To set the axis ranges in Matplotlib, users can use the set_xlim() and set_ylim() functions, which allow for precise control over the range of values displayed on the graph. This feature is especially useful in cases where the dataset has a wide range of values or when comparing multiple datasets with different scales. Overall, the axis range setting capability in Matplotlib provides users with greater flexibility and control in creating visually appealing and informative graphs.
Set Axis Ranges in Matplotlib
You can use the following syntax to set the axis ranges for a plot in Matplotlib:
#specify x-axis range plt.xlim(1, 15) #specify y-axis range plt.ylim(1, 30)
The following examples show how to use this syntax in practice.
Example 1: Specify Both Axes Ranges
The following code shows how to specify the range for both axes:
import matplotlib.pyplotas plt #define x and y x = [1, 4, 10] y = [5, 11, 27] #create plot of x and y plt.plot(x, y) #specify x-axis and y-axis range plt.xlim(1, 15) plt.ylim(1, 30)

Example 2: Specify Range for X-Axis Only
The following code shows how to specify the range for the x-axis only:
import matplotlib.pyplotas plt #define x and y x = [1, 4, 10] y = [5, 11, 27] #create plot of x and y plt.plot(x, y) #specify x-axis range plt.xlim(1, 15)

Example 3: Specify Range for Y-Axis Only
The following code shows how to specify the range for the y-axis only:
import matplotlib.pyplotas plt #define x and y x = [1, 4, 10] y = [5, 11, 27] #create plot of x and y plt.plot(x, y) #specify y-axis range plt.ylim(1, 30)

Cite this article
stats writer (2024). How can the axis ranges be set in Matplotlib?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-the-axis-ranges-be-set-in-matplotlib/
stats writer. "How can the axis ranges be set in Matplotlib?." PSYCHOLOGICAL SCALES, 3 May. 2024, https://scales.arabpsychology.com/stats/how-can-the-axis-ranges-be-set-in-matplotlib/.
stats writer. "How can the axis ranges be set in Matplotlib?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-the-axis-ranges-be-set-in-matplotlib/.
stats writer (2024) 'How can the axis ranges be set in Matplotlib?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-the-axis-ranges-be-set-in-matplotlib/.
[1] stats writer, "How can the axis ranges be set in Matplotlib?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can the axis ranges be set in Matplotlib?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
