Skip to content Skip to sidebar Skip to footer

Plot A Graph, Clear Its Axes, Then Plot A New Graph

I'm trying to do the following: create a figure, plot a graph on it, then in 3 seconds clear its axes. When that happen a new graph should be plotted on the same figure and it shou

Solution 1:

If you want to animate your figures, you can use matplotlib.animation library. Here is what your code would look like:

import matplotlib.pyplot as plt
import time
import matplotlib.animation as animation

fig = plt.figure()
ax = fig.add_subplot(111)
line, = ax.plot([],[])
ax.set_xlim(3)
ax.set_ylim(3)
line.set_data([1,2,3],[1,2,3])

definit():
            """ Initializes the plots to have zero values."""
            line.set_data([],[])
            return line,

defanimate(n, *args, **kwargs):
    if(n%2==0):
        line.set_data([],[])
    else:   
        line.set_data([1,2,3],[1,2,3])

    return line,

ani = animation.FuncAnimation(fig, animate, init_func=init,frames =100, interval=10, blit=False, repeat =False)
fig.show()

Look into matplotlib.animation for more details. This link can get you started.

Post a Comment for "Plot A Graph, Clear Its Axes, Then Plot A New Graph"