Reputation: 357
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!
Upvotes: 4
Views: 1822
Reputation: 284830
There is a workaround for PatchCollection
not having a working remove
method.
The arrows are added to ax.patches
. Assuming you don't have any other patches in the plot (e.g. a bar plot uses patches), you can just do ax.patches = []
to remove the arrows.
Ideally, you'd get the patches from the sl.arrows
PatchCollection
, but it doesn't actually store a reference to the patches themselves, just their raw paths.
If you do have other patches in the plot, you could remove all instances of FancyArrowPatch
instead. E.g.
keep = lambda x: not isinstance(x, mpl.patches.FancyArrowPatch)
ax.patches = [patch for patch in ax.patches if keep(patch)]
Upvotes: 2