是否有axes.clear()的替代方法,可在擦除轴内容的同时保持轴标签不变?
上下文:我有一个交互式脚本,该脚本可翻阅一些流图,并使用axis.quiver()对每幅图进行绘制。如果在两次轴调用之间不调用axis.clear(),则每次quiver()调用只会向图中添加更多箭头,而无需先擦除之前添加的箭头。但是,当我调用axes.clear()时,它使轴标签变形。我可以重新设置它们,但这有点烦人。
您可以使用的艺术家从轴中删除remove()艺术家。下面的代码显示了这样做的两个选项。
import matplotlib.pyplot as plt
import numpy as np
X, Y = np.meshgrid(np.arange(0, 2 * np.pi, .2), np.arange(0, 2 * np.pi, .2))
U = np.cos(X)
V = np.sin(Y)
plt.figure()
plt.title('Arrows scale with plot width, not view')
plt.xlabel('xlabel')
plt.xlabel('ylabel')
Q = plt.quiver(X, Y, U, V, units='width')
l, = plt.plot(X[0,:], U[4,:]+2)
# option 1, remove single artists
#Q.remove()
#l.remove()
# option 2, remove all lines and collections
for artist in plt.gca().lines + plt.gca().collections:
artist.remove()
plt.show()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3761 次 |
| 最近记录: |