无法从Matplotlib轴中删除streamplot箭头头部

3
如何在不清除所有内容(即不使用 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

下图展示了一个例子。左边是流线图,右边是剩余箭头头部。

Streamplot example: full plot (left), and lines removed (right)


为了解释背景,我正在尝试将流线添加到现有的imshow动画中(使用matplotlib.animation.FuncAnimation构建)。 在这种设置中,每帧仅更新图像数据,并且无法清除并重新绘制整个图。

1个回答

4

这个解决方案看起来可行,受到这个答案的启发。有两种方法:

  1. 删除箭头补丁
  2. 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

enter image description here


谢谢,这个可行!我希望有一种方法可以专门针对流场箭头而不是所有箭头进行操作,但正如您链接的答案所解释的那样,这可能是不可能的。 - Arcturus B

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接