Open In App

Matplotlib.pyplot.axis() in Python

Last Updated : 27 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

axis() function in Matplotlib is used to get or set properties of the x- and y-axis in a plot. It provides control over axis limits, aspect ratio and visibility, allowing customization of the plot’s coordinate system and view. It's key feature includes:

  • Gets or sets the axis limits [xmin, xmax, ymin, ymax].
  • Can set the aspect ratio of the plot ('equal', 'auto', or a numeric ratio).
  • Supports toggling axis visibility ('on' or 'off').
  • Allows tight layout around data with 'tight'.

Example:

Python
import matplotlib.pyplot as plt

plt.plot([1, 2, 3], [4, 5, 6])
print(plt.axis())

Output

(np.float64(0.9), np.float64(3.1), np.float64(3.9), np.float64(6.1))
Output
Using Matplotlib.pyplot.axis()

Explanation: plt.axis() without arguments returns the current axis limits [xmin, xmax, ymin, ymax], showing the automatically determined x and y ranges framing the plot.

Syntax

matplotlib.pyplot.axis(*args, **kwargs)

Parameters:

1. No arguments: Returns current axis limits [xmin, xmax, ymin, ymax].

2. List/tuple of 4 floats: Sets axis limits [xmin, xmax, ymin, ymax].

3. String options:

  • 'on' / 'off': show/hide axis
  • 'equal': equal scaling (1:1 aspect ratio)
  • 'auto': automatic limits
  • 'tight': tight limits around data

4. Numeric value: Sets manual aspect ratio (e.g., plt.axis(2)).

Examples

Example 1: In this example, we are passing custom axis limits to manually define the visible range of the plot on both x and y axes.

Python
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])

plt.axis([0, 5, 0, 10])    # Set x-axis: 0–5, y-axis: 0–10
plt.title("Custom Axis Limits")
plt.show()

Output

Output
Using Matplotlib.pyplot.axis()

Explanation: plt.axis([0, 5, 0, 10]) sets the x-axis from 0 to 5 and y-axis from 0 to 10, manually defining the plot’s visible range.

Example 2: This example we turn off the axis lines and labels, making the plot display without any visible axes.

Python
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])

plt.axis('off')     # Turn off axis lines and labels
plt.title("Axis Hidden")
plt.show()

Output

Output
Using Matplotlib.pyplot.axis()

Explanation: plt.axis('off') hides the axis lines and labels, making the plot display without any axis markings or ticks.

Example 3: This example set the aspect ratio so that one unit on the x-axis is equal in length to one unit on the y-axis, preserving the true proportions of the data.

Python
import matplotlib.pyplot as plt

plt.plot([0, 1], [0, 2])
plt.axis('equal')   # Equal scaling for x and y axes
plt.title("Equal Aspect Ratio")
plt.show()

Output

Output
Using Matplotlib.pyplot.axis()

Explanation: plt.axis('equal') sets equal scaling on both axes, ensuring one unit on the x-axis equals one unit on the y-axis.

Example 4: In this example we automatically adjusts the axis limits to fit closely around the plotted data, minimizing any extra whitespace around the plot.

Python
import matplotlib.pyplot as plt

plt.plot([10, 20, 30], [1, 2, 3])
plt.axis('tight')   # Fit axes tightly around data points
plt.title("Tight Layout")
plt.show()

Output

Output
Using Matplotlib.pyplot.axis()

Explanation: plt.axis('tight') adjusts the axis limits to fit closely around the plotted data, minimizing extra space.


Next Article
Article Tags :
Practice Tags :

Similar Reads