如何清除以前绘制的Matplotlib文本框?
要清除以前绘制的Matplotlib文本框,可以执行以下步骤 –
- 设置图形大小并调整子图之间和周围的填充。
- 使用numpy创建 x 和 y 数据点。
- 使用 plot() 方法绘制 x 和 y 。
- 在图中放置字符标记。
- 要清除文本,请使用 text.remove() ,其中 text 是返回的艺术家。
- 要显示图形,请使用 show() 方法。
示例
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()
输出
现在,取消注释 “text.remove()” 并再次执行代码。它将产生以下输出。