Is there a way to make it so plots made on matplotlib will cycle like a movie? If I have 12 different plots and having it show the first for 10 seconds, then the next and so on, then repeating after the last one? I would like to do this as an alternative to subplots due to the number of plots I want to show.
1 Answer
yes, Using animation module with repeat = True and by modifying the interval argument to control the time between plots.
import matplotlib.animation as animation
fig = plt.figure()#
data_, = plot([], [])
def data_gen():
while i < NUMBER_OF_PLOTS:
'''
make your instance of X data in a list called YOUR_X_DATA
and your Y data in a list called YOUR_Y_DATA
and iterate along it.
'''
yield YOUR_X_DATA[i], YOUR_Y_DATA[i]
i+=1
def run(data):
data_.set_data(data[0], data[1])
ani = animation.FuncAnimation(fig , run, data_gen, interval=100,repeat=True)
plt.show()
OR using time.sleep(10) and plt.close(fig) this way
import time
# suppose you have fig1, fi2, fig 3 etc.
while(True):
time.sleep(10)
plt.close(fig1)
# import the next plot
time.sleep(10)
plt.close(fig2)
# import the next plot
time.sleep(10)
plt.close(fig3)
8 Comments
TheStrangeQuark
This should work. Would it be possible to make it so it changes everytime someone clicks a mouse or presses space?
farhawa
you mean ""go to the next graph" by "change"?
TheStrangeQuark
I'm also still a little confused on your first example. Where do I put the different plots I want to be shown?
farhawa
@greenthumbtack take alook to the comment I added. sorry
TheStrangeQuark
So YOUR_X_DATA is a list of lists of my data for the different plots?
|
animationmodule, take a look at this example: matplotlib.org/1.4.2/examples/animation/basic_example.html