如何在不清除所有内容(即不使用plt.cla()
或plt.clf()
的情况下从Matplotlib图中删除streamplot?
plt.streamplot()
返回一个StreamplotSet
(在下面的示例中为streams
),其中包含流线(.lines
)和箭头(.arrows
)。
呼叫streams.lines.remove()
会按预期删除流线型。
但是,我找不到移除箭头的方法:stream.arrows.remove()
引发NotImplementedError
,而stream.arrows.set_visible(False)
无效。
import matplotlib.pyplot as plt
import numpy as np
# Generate streamplot data
x = np.linspace(-5, 5, 10)
y = np.linspace(-5, 5, 10)
u, v = np.meshgrid(x, y)
# Create streamplot
streams = plt.streamplot(x, y, u, v)
# Remove streamplot
streams.lines.remove() # Removes the stream lines
streams.arrows.set_visible(False) # Does nothing
streams.arrows.remove() # Raises NotImplementedError
下图说明了示例。 左:流图,右:其余箭头。
对于上下文,我正在尝试将流线添加到现有的imshow动画(使用matplotlib.animation.FuncAnimation
构建)中。
在此设置中,每帧仅更新图像数据,而我无法清除并重新绘制全部图。
答案 0 :(得分:2)
此解决方案似乎有效,并且受到this答案的启发。有两种方法:
alpha
参数设置为0 streams = plt.streamplot(x, y, u, v)
ax = plt.gca()
for art in ax.get_children():
if not isinstance(art, matplotlib.patches.FancyArrowPatch):
continue
art.remove() # Method 1
# art.set_alpha(0) # Method 2