Matplotlib.axes.Axes.clear() in Python

Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.
matplotlib.axes.Axes.clear() Function
The Axes.clear() function in axes module of matplotlib library is used to clear the axes.
Syntax: Axes.clear(self)
Parameters: This method does not accept any parameters.
Returns:This method does not returns any values.
Below examples illustrate the matplotlib.axes.Axes.clear() function in matplotlib.axes:
Example 1:
# Implementation of matplotlib functionimport numpy as npimport matplotlib.pyplot as plt fig, ax = plt.subplots()ax.set_xlabel('x-axis')ax.set_ylabel('y-axis')ax.plot([1, 2, 3])ax.grid(True)ax.clear()ax.set_title('matplotlib.axes.Axes.clear() \Example\n', fontsize = 12, fontweight ='bold')plt.show() |
Output:
Example 2:
# Implementation of matplotlib functionimport numpy as npimport matplotlib.pyplot as plt t = np.linspace(0.0, 2.0, 201)s = np.sin(2 * np.pi * t) fig, [ax, ax1] = plt.subplots(2, 1, sharex = True) ax.set_ylabel('y-axis')ax.plot(t, s)ax.grid(True)ax.set_title('matplotlib.axes.Axes.clear() Example\n\n Sample Example', fontsize = 12, fontweight ='bold') ax1.set_ylabel('y-axis')ax1.plot(t, s)ax1.grid(True)ax1.clear()ax1.set_title('Above example with clear() \function', fontsize = 12, fontweight ='bold')plt.show() |
Output:




