1

I am trying to plot a point whose position is controlled by a slider. However, each time the slider is moved a new point is plotted without deleting the old one. How do you remove the old point before the new one is plotted?

from mpl_toolkits.mplot3d import Axes3D from matplotlib import pyplot from matplotlib import pyplot as plt from matplotlib.widgets import Slider def on_change(val): point=ax.scatter(x[int(val)/1],y[int(val)/1],z[int(val)/1]) x=[0,0.5,1] y=[0,0.5,1] z=[0,0.5,1] p=0 fig = pyplot.figure() ax = fig.add_subplot(111, projection='3d') ax.plot([0,0,0,0,0,1,1,0,0,1,1,1,1,1,1,0],[0,1,1,0,0,0,0,0,1,1,0,0,1,1,1,1],[0,0,1,1,0,0,1,1,1,1,1,0,0,1,0,0],c='black',zorder=10) point=ax.scatter(x[p],y[p],z[p],zorder=0) ax.set_xlabel('Width') ax.set_ylabel('Depth') ax.set_zlabel('Height') slider_ax = plt.axes([0.15, 0.05, 0.7, 0.02]) slider = Slider(slider_ax, "min", 0, 2, valinit=1, color='blue') slider.on_changed(on_change) pyplot.show() 
5
  • You're missing some imports. Please add them. Is plt pyplot? If so, then why do you also use pyplot? Usually people do from matplotlib import pyplot as plt but I'm not sure what your convention is. Commented Jul 23, 2014 at 21:56
  • I fixed the imports and it didn't solve the problem. Commented Jul 23, 2014 at 22:14
  • But now others can run your code. The issue is that you're drawing a subplot; usually, when you call plt.scatter, it returns a handle to the points drawn. i.e. points = plt.scatter(whatever) Here, you're calling scatter, but since it's a subplot, you're scattering on an axis. And the handle that the axis returns does not have a remove method. You could just try redrawing the entire thing. But that's not ideal. I'm seeing if I can figure something out. Commented Jul 23, 2014 at 22:22
  • 1
    @Daryl Not sure what you are talking about. The PathCollection returned by scatter definitely has a remove method. Commented Jul 24, 2014 at 1:43
  • Hmm I guess the remove method didn't do anything for me when I tried it - at any rate HYRY probably has solved your problem. Commented Jul 24, 2014 at 16:27

1 Answer 1

1

You can change the properties of point:

def on_change(val): point.set_offsets([x[int(val) / 1], y[int(val) / 1]]) point.set_3d_properties([z[int(val) / 1]], "z") fig.canvas.draw() 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.