3

i've always found help here until now. i've been looking for a solution of my problem very long and i might be blind by now.. i hope you can help me with this one:

i've built a python-program that plots either the direction field (quiver) or the streamplot. since there might be other data in the graph (e.g. trajectories) i can't just clear everything and replot. instead i want to delete single elements. this works perfect for everything except the streamplot.

so, the streamplot consists of lines and arrows. stored in the variable sl i can simply call sl.lines.remove() to delete the lines. this doesn't work for the arrows, though. how do i delete these?

edit: so here's a little code:

import numpy as np
import matplotlib.pyplot as pp

streamplot = None

def mySystem(z):
    x, y = z
    return y, -x


def stream():
    xmin = -10
    xmax = 10
    ymin = -10
    ymax = 10

    N = 40
    M = int(N)
    a = np.linspace(xmin,xmax,N)
    b = np.linspace(ymin,ymax,N)

    X1, Y1 = np.meshgrid(a,b)
    DX1, DY1 = mySystem([X1,Y1])

    global streamplot
    streamplot = pp.streamplot(X1, Y1, DX1, DY1, density=2, color='#b5b5b5')

def removeStream():
    global streamplot
    streamplot.lines.remove()

    #doesn't work: streamplot.arrows.remove()

stream()
removeStream()
pl.show() # arrows still here!
4

1 回答 1

2

PatchCollection没有工作方法有一种解决remove方法。

箭头添加到ax.patches。假设您在图中没有任何其他补丁(例如条形图使用补丁),您可以ax.patches = []删除箭头。

理想情况下,您会从 中获取补丁sl.arrows PatchCollection,但它实际上并不存储对补丁本身的引用,而只是存储它们的原始路径。

如果图中确实有其他补丁,则可以删除所有实例FancyArrowPatch。例如

keep = lambda x: not isinstance(x, mpl.patches.FancyArrowPatch)
ax.patches = [patch for patch in ax.patches if keep(patch)]
于 2013-10-27T18:57:43.200 回答