To clear a Matplotlib textbox that was previously drawn, we can take the following steps −
- Set the figure size and adjust the padding between and around the subplots.
- Create x and y data points using numpy.
- Plot x and y using plot() method.
- Place characters token on the plot.
- To clear the text, use text.remove(), where text is a returned artist.
- To display the figure, use show() method.
Example
import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() x = np.linspace(-10, 10, 100) y = np.sin(x) ax.plot(x, y) text = fig.text(0.5, 0.96, "$y=sin(x)$") #text.remove() plt.show()
Output
Now, uncomment the line "text.remove()" and execute the code again. It will produe the following output.