python - Preserve axis settings between FuncAnimation frames -
i want generate video of animated series of line plots different files. below start python code. using matplotlib.animation library.
it looks this:
how can erase previous plots (show 1 line @ time), keep same axes , plot size?
if call plt.clf(), resets axes.
#! /usr/bin/env python __future__ import print_function import numpy np import matplotlib.pyplot plt import matplotlib.animation animation def plot_initialize(): plt.xlim(0, 6) plt.ylim(0, 35) plt.axis([0, 6, 0, 35]) def plot_figure(filepath): print(filepath) columns = np.loadtxt(filepath, unpack=true) x = columns[0] y = columns[1] plt.plot(x, y, color="blue", linewidth=1.0, linestyle="-") if __name__ == '__main__': import tempfile myfile1 = tempfile.namedtemporaryfile() myfile1.write('# myfile1\n1 2\n2 4\n3 6\n4 8\n5 10\n') myfile1.seek(0) myfile2 = tempfile.namedtemporaryfile() myfile2.write('# myfile2\n1 1\n2 4\n3 9\n4 16\n5 25\n') myfile2.seek(0) myfile3 = tempfile.namedtemporaryfile() myfile3.write('# myfile3\n1 2\n2 4\n3 8\n4 16\n5 32\n') myfile3.seek(0) filepaths = [myfile1.name, myfile2.name, myfile3.name] my_figure = plt.figure() anim = animation.funcanimation(my_figure, plot_figure, init_func=plot_initialize, frames=filepaths, interval=500, repeat=false) anim.save("out.mp4")
it might make sense update data in animation , not create new plots every time
from __future__ import print_function import numpy np import matplotlib.pyplot plt import matplotlib.animation animation def plot_initialize(): return def plot_figure(filepath): print(filepath) columns = np.loadtxt(filepath, unpack=true) x = columns[0] y = columns[1] line.set_data(x,y) if __name__ == '__main__': import tempfile myfile1 = tempfile.namedtemporaryfile() myfile1.write('# myfile1\n1 2\n2 4\n3 6\n4 8\n5 10\n') myfile1.seek(0) myfile2 = tempfile.namedtemporaryfile() myfile2.write('# myfile2\n1 1\n2 4\n3 9\n4 16\n5 25\n') myfile2.seek(0) myfile3 = tempfile.namedtemporaryfile() myfile3.write('# myfile3\n1 2\n2 4\n3 8\n4 16\n5 32\n') myfile3.seek(0) filepaths = [myfile1.name, myfile2.name, myfile3.name] my_figure = plt.figure() plt.xlim(0, 6) plt.ylim(0, 35) plt.axis([0, 6, 0, 35]) line, = plt.plot([], [], color="blue", linewidth=1.0, linestyle="-") anim = animation.funcanimation(my_figure, plot_figure, init_func=plot_initialize, frames=filepaths, interval=500, repeat=false) anim.save("out.mp4") 
Comments
Post a Comment