Python:Matplotlib .axis()
The .axis() function in Matplotlib’s pyplot module gets or sets properties of the plot axes, including axis limits, aspect ratio, and visibility.
Syntax
matplotlib.pyplot.axis(arg=None, emit=True, **kwargs)
The function can be called in several ways:
axis(): Returns current axis limits as(xmin, xmax, ymin, ymax)axis([xmin, xmax, ymin, ymax]): Sets axis limitsaxis(option): Sets axis properties using predefined optionsaxis(**kwargs): Sets axis properties using keyword arguments
Parameters:
arg(optional): Defines how the axes are set or displayed. Accepts the following values:- A list or tuple
[xmin, xmax, ymin, ymax]to set axis limits. 'off'to hide the axes.'on'to show the axes.'equal','scaled','tight','auto','image','square'— control axis scaling and aspect ratio.
- A list or tuple
emit(bool, default: True): IfTrue, notifies observers of axis limit changes.kwargs(optional): Additional axis properties such asxmin,xmax,ymin, andymax.
Return value:
Returns a tuple (xmin, xmax, ymin, ymax) representing the current axis limits.
Example 1: Using .axis() to Control Axis Properties
This example creates three subplots demonstrating different axis control modes. The first subplot uses axis([2, 8, -0.5, 0.5]) to restrict the view to specific x and y ranges. The second subplot uses axis('equal') to ensure both axes use the same scale. The third subplot uses axis('off') to hide the axis lines, ticks, and labels:
import matplotlib.pyplot as pltimport numpy as np# Generate sample datax = np.linspace(0, 10, 100)y = np.sin(x)# Create a basic plotplt.figure(figsize=(10, 4))# Subplot 1: Custom axis limitsplt.subplot(1, 3, 1)plt.plot(x, y)plt.axis([2, 8, -0.5, 0.5])plt.title('Custom Limits')plt.grid(True, alpha=0.3)# Subplot 2: Equal aspect ratioplt.subplot(1, 3, 2)plt.plot(x, y)plt.axis('equal')plt.title('Equal Scaling')plt.grid(True, alpha=0.3)# Subplot 3: Axis turned offplt.subplot(1, 3, 3)plt.plot(x, y)plt.axis('off')plt.title('Hidden Axes')plt.tight_layout()plt.show()
The output of this code is:

Example 2: Getting and Setting Axis Limits
This example demonstrates how to set custom axis limits and retrieve the current limits. The axis() function is called with a list of four values to set the x and y axis ranges, then called without arguments to return the current limits as a tuple.
import matplotlib.pyplot as pltimport numpy as np# Create datax = np.linspace(0, 2*np.pi, 100)y = np.sin(x)# Create plotplt.plot(x, y, linewidth=2)plt.title('Sine Wave with Custom Axes')# Set custom axis limitsplt.axis([0, 2*np.pi, -1.5, 1.5])# Get and print current axis limitslimits = plt.axis()print(f"Current axis limits: {limits}")plt.grid(True, alpha=0.3)plt.show()
The output of this code is:

Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:Matplotlib on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours