2

I am new to using PyLab. I want to plot some points. But I don't want to show the previous points i.e. as a new point comes the previous plotted point will vanish and the new point will be plotted. I have searched a lot but I could not find how to re-initialize the plot in between. The problem I am facing is I can set the current figure by using plt.figure(f1.number) but after plotting the point in that figure it gets permanently changed.

Pranjal Sahu
  • 1,469
  • 3
  • 18
  • 27

2 Answers2

6

plt.hold(False) before you start plotting will do what you want.

hold determines of old artists are held-on to when new ones are plotted. The default is for hold to be on.

ex

# two lines 
plt.figure()
plt.hold(True)
plt.plot(range(5))
plt.plot(range(5)[::-1])

#one line
plt.figure()
plt.hold(False)
plt.plot(range(5))
plt.plot(range(5)[::-1])

Changing it via plt.hold changes it for all (new) axes. You can change the hold state for an individual axes by

ax = gca()
ax.hold(True)
tacaswell
  • 84,579
  • 22
  • 210
  • 199
  • I think this will do. Thanks :) – Pranjal Sahu Feb 18 '13 at 10:21
  • @sahu If this solved your problem could you accept it? That marks the question as solved so future user with a similar problem will know that you found a reasonable answer. http://stackoverflow.com/faq#howtoask – tacaswell Feb 18 '13 at 15:07
  • I would request you to see the video link [Online clustering](http://www.youtube.com/watch?v=TYxwjO4TsvQ). Can you explain how this animation was made ? – Pranjal Sahu Feb 18 '13 at 19:29
  • @sahu This really should be it's own question. You just render each frame and then stitch them together. You can do this either by saving each frame individually and stitching them via ffmeg on using the [`animation` api](http://matplotlib.org/api/animation_api.html). There are a number of questions on SO about this. – tacaswell Feb 18 '13 at 19:53
1

With pylab, pylab.clf() should clear the figure, after which you can redraw the plot.

Alternatively, you can update your data with set_xdata and set_ydata that are methods on the axes object that gets returned when you create a new plot (either with pylab.plot or pylab.subplot).

The latter is probably preferred, but requires a litte more work. One example I can quickly find is another SO question.

Community
  • 1
  • 1
  • The problem with clf() approach was that the plot was flickering when new points were arriving. I want this to happen like some animation i.e. as points arrive the older will disappear.Basically I want to implement something like this [Online Clustering](http://www.youtube.com/watch?v=TYxwjO4TsvQ) btw the link given was useful thanks :) – Pranjal Sahu Feb 18 '13 at 10:22