简体   繁体   中英

Re-initialize the plot in pylab

I am new to using PyLab. I want to plot some points. But I don't want to show the previous points ie 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.

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)

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM