Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
476 views
in Technique[技术] by (71.8m points)

numpy - Why doesn't python list remove() work for list of plots?

I'm trying to do some animation with FuncAnimation. Instead of using .set_data() I'm using this alternative structure:

def update_plot(frame, y, plot):
    plot[0].remove()
    plot[0] = ax.scatter(np.sin(y[frame,0]),-np.cos(y[frame,0]), color = "orange")

#(...)

# Initial
plot = [ax.scatter(np.sin(y[0,0]),-np.cos(y[0,0]), color = "orange")]

# Animate
animate = animation.FuncAnimation(fig, update_plot, nmax, fargs = (y, plot))
animate.save('pendulum.gif',writer='imagemagick')

This works well. However, if I use ax.plot() instead of ax.scatter():

def update_plot(frame, y, plot):
    plot[0].remove()
    plot[0] = ax.plot(np.sin(y[frame,0]),-np.cos(y[frame,0]),'o', color = "orange")

# Initial
plot = [ax.plot(np.sin(y[0,0]),-np.cos(y[0,0]),'o', color = "orange")]

# Animate
animate = animation.FuncAnimation(fig, update_plot, nmax, fargs = (y, plot))
animate.save('pendulum.gif',writer='imagemagick')

the code fails because of the plot[0].remove() with the following error:

remove() takes exactly one argument (0 given)

Why is that the case?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

OK, so here is the line you need:

ax.lines.clear()

This is because ax retains its own list of things it is plotting, and anything you do to your own lists won't affect that. So, this line removes all the lines, and then you can start adding new ones.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...