Axes.set() function in Matplotlib is used to set multiple Axes properties at once using keyword arguments (**kwargs). This is a convenient way to configure an Axes object with labels, limits, title and more, all in one call. It's key features include:
- Acts as a batch property setter for Axes.
- Uses keyword arguments to set multiple properties like title, labels, axis limits, etc.
- Commonly used in plots to quickly configure appearance.
- Modifies the Axes object in-place.
Example:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [4, 5, 6])
ax.set(
xlabel='X Axis Label',
ylabel='Y Axis Label',
title='Simple Line Plot'
)
plt.show()
Output

Explanation:
- fig, ax = plt.subplots() creates the figure and plotting area.
- ax.plot(...) draws the line plot with given x and y data.
- ax.set(...) sets multiple properties like axis labels and title in one call for cleaner code.
Syntax
Axes.set(self, **kwargs)
Parameters: **kwargs is a keyword arguments representing property names and their values (e.g., xlabel, ylabel, title, xlim, ylim, etc.)
Returns: The modified Axes object.
Examples of matplotlib.axes.Axes.set()
Example 1: In this example, we plot a sine wave and use the ax.set() method to configure multiple properties of the Axes object in one call.
import matplotlib.pyplot as plt
import numpy as np
t = np.arange(0.0, 2, 0.001)
s = 1 + np.sin(8 * np.pi * t) * 0.4
fig, ax = plt.subplots()
ax.plot(t, s)
ax.set(xlabel='X-Axis', ylabel='Y-Axis',
xlim=(0, 1.5), ylim=(0.5, 1.5),
title='matplotlib.axes.Axes.set() function Example')
ax.grid()
plt.show()
Output

Explanation: This code calculates a sine wave scaled by 0.4 and shifted by 1 to create oscillations around 1, then initializes a figure and axes for plotting. It plots the sine wave against time and uses ax.set() to simultaneously set axis labels, axis limits and the plot title.
Example 2 : In this example, we create a scatter plot with randomly generated data points using NumPy and Matplotlib.
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(19680801)
fig, ax = plt.subplots()
x, y, s, c = np.random.rand(4, 200)
s *= 200
ax.scatter(x, y, s, c)
ax.set(xlabel='X-Axis', ylabel='Y-Axis',
xlim=(0, 0.5), ylim=(0, 0.5))
ax.grid()
plt.show()
Output

Explanation: This code initializes a figure and axes for plotting, then generates 200 random points for x and y coordinates, along with sizes and colors for each scatter point. It scales the sizes, plots the scatter points and uses ax.set() to set axis labels and limit the displayed range.