Matplotlib.pyplot.axis() in Python
Last Updated :
27 May, 2025
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))
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
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
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
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
Using Matplotlib.pyplot.axis()Explanation: plt.axis('tight') adjusts the axis limits to fit closely around the plotted data, minimizing extra space.
Similar Reads
Matplotlib.pyplot.axes() in Python
Pyplot is another module of matplotlib that enables users to integrate MATLAB within Python environment, and thus providing MATLAB like interface and making Python visually interactive. Matplotlib.pyplot.axes() pyplot.axes is a function of the matplotlib library that adds axes to the current graph a
2 min read
Matplotlib.pyplot.axvspan() in Python
Matplotlib is a plotting library for creating static, animated, and interactive visualizations in Python.\ Pyplot is a Matplotlib module which provides a MATLAB-like interface. Matplotlib is designed to be as usable as MATLAB, with the ability to use Python and the advantage of being free and open-s
2 min read
Matplotlib.pyplot.delaxes() in Python
Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. matplotlib.pyplot.delaxes() Function The delaxes() function in pyplot module of matplotlib library is use
2 min read
Matplotlib.pyplot.axhspan() in Python
Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. matplotlib.pyplot.axhspan() Function The axhspan() function in pyplot module of matplotlib library is use
2 min read
Matplotlib.pyplot.axvline() in Python
Matplotlib is a plotting library for creating static, animated, and interactive visualizations in Python. Matplotlib can be used in Python scripts, the Python and IPython shell, web application servers, and various graphical user interface toolkits like Tkinter, awxPython, etc. Note: For more inform
3 min read
matplotlib.pyplot.axhline() in Python
Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. matplotlib.pyplot.axhline() Function The axhline() function in pyplot module of matplotlib library is use
2 min read
Matplotlib.pyplot.gca() in Python
Matplotlib is a library in Python and it is a numerical - mathematical extension for the NumPy library. Pyplot is a state-based interface to a Matplotlib module that provides a MATLAB-like interface.  matplotlib.pyplot.gca() Function The gca() function in pyplot module of matplotlib library is used
2 min read
Matplotlib.pyplot.cla() in Python
Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot,
1 min read
Matplotlib.pyplot.csd() in Python
Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. matplotlib.pyplot.csd() Function The csd() function in pyplot module of matplotlib library is used to plo
3 min read
Matplotlib.pyplot.draw() in Python
Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. matplotlib.pyplot.draw() Function The draw() function in pyplot module of matplotlib library is used to r
1 min read