Matplotlib.pyplot.axes() in Python
Last Updated :
31 May, 2025
axes() method in Matplotlib is used to create a new Axes instance (i.e., a plot area) within a figure. This allows you to specify the location and size of the plot within the figure, providing more control over subplot layout compared to plt.subplot(). It's key features include:
- Creates a new Axes at a specified position inside the figure.
- Allows precise control of the Axes position using normalized figure coordinates.
- Returns an Axes object for further customization and plotting.
- Supports multiple Axes in a single figure for complex layouts.
Example:
Python
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = plt.axes([0.1, 0.1, 0.8, 0.8])
x = np.linspace(0, 10, 100)
y = np.sin(x)
ax.plot(x, y)
plt.show()
Output
Using Matplotlib.pyplot.axes()Explanation:
- plt.figure() creates a blank figure and plt.axes([0.1, 0.1, 0.8, 0.8]) adds a subplot positioned within it.
- np.linspace(0, 10, 100) creates 100 points and np.sin(x) computes their sine values.
- ax.plot(x, y) plots the sine wave.
Syntax
matplotlib.pyplot.axes(*args, **kwargs)
Parameters: The parameters can be passed in different ways:
1. Positional Argument rect: A list or tuple of 4 floats [left, bottom, width, height] and defines the position and size of the axes as fractions of the figure width and height, all values between 0 and 1.
- left: The distance from the left side of the figure to the left side of the axes.
- bottom: The distance from the bottom of the figure to the bottom of the axes.
- width: The width of the axes.
- height: The height of the axes.
2. Keyword Arguments (kwargs): A dictionary of additional keyword arguments to customize the Axes object. Some common keyword arguments include:
- polar: A boolean value (True or False). If True, creates a polar plot.
- facecolor: A color value to set the background color of the axes.
- projection: A string specifying the projection type (e.g., 'polar').
- Other Axes properties can also be passed as keyword arguments for further customization.
Returns: (Axes) An instance of matplotlib.axes.Axes (or a subclass like Axes3D) .The newly created Axes object which can be used to plot data and customize the plot.
Examples
Example 1: In this example we are creating multiple axes in one figure.
Python
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax1 = plt.axes([0.1, 0.1, 0.35, 0.8]) # Left small axes
ax2 = plt.axes([0.55, 0.1, 0.35, 0.8]) # Right small axes
x = np.linspace(0, 10, 100)
ax1.plot(x, np.sin(x))
ax2.plot(x, np.cos(x))
plt.show()
Output
Using Matplotlib.pyplot.axes()Explanation:
- plt.figure() initializes a blank figure and plt.axes() adds two Axes objects (ax1 and ax2) with specified positions using normalized figure coordinates [left, bottom, width, height].
- ax1 (left side) is 10% from left, 10% from bottom, 35% width, 80% height, and ax2 (right side) is 55% from left, 10% from bottom, 35% width, 80% height.
- np.linspace(0, 10, 100) generates 100 evenly spaced x-values between 0 and 10.
Example 2: In this example, we are creating 3d axes
Python
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
fig = plt.figure()
ax = plt.axes([0.1, 0.1, 0.8, 0.8], projection='3d')
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X**2 + Y**2))
ax.plot_surface(X, Y, Z, cmap='viridis')
plt.show()
Output
Using Matplotlib.pyplot.axes()Explanation:
- plt.axes() creates a 3D Axes at position [0.1, 0.1, 0.8, 0.8] with projection='3d'.
- np.linspace(-5, 5, 100) generates 100 points for both x and y and np.meshgrid(x, y) creates a 2D grid.
- np.sin(np.sqrt(X**2 + Y**2)) calculates height values as the sine of the distance from the origin.
- ax.plot_surface() plots the 3D surface using the viridis colormap.
Similar Reads
Matplotlib.pyplot.axis() in Python 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,
3 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.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.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.draw() in Python matplotlib.pyplot.draw() function redraw the current figure in Matplotlib. Unlike plt.show(), it does not block the execution of code, making it especially useful in interactive sessions, real-time visualizations where the plot needs to update dynamically without pausing the program. Example: Python
2 min read
Matplotlib.axes.Axes.plot() in Python Axes.plot() method in Matplotlib is used to plot data on a set of axes. It is primarily used for creating line plots but can be extended for other types of plots, including scatter plots, bar plots, and more. When using this function, you typically provide the x and y coordinates of the data points
3 min read
matplotlib.pyplot.figure() 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,
2 min read
Matplotlib.pyplot.sca() 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