惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The GitHub Blog
The GitHub Blog
IT之家
IT之家
B
Blog RSS Feed
罗磊的独立博客
GbyAI
GbyAI
博客园 - Franky
Y
Y Combinator Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
博客园 - 聂微东
N
Netflix TechBlog - Medium
博客园 - 三生石上(FineUI控件)
人人都是产品经理
人人都是产品经理
U
Unit 42
博客园 - 叶小钗
Jina AI
Jina AI
MyScale Blog
MyScale Blog
雷峰网
雷峰网
B
Blog
Hugging Face - Blog
Hugging Face - Blog
Blog — PlanetScale
Blog — PlanetScale
Recent Announcements
Recent Announcements
腾讯CDC
酷 壳 – CoolShell
酷 壳 – CoolShell

Comments for all models are wrong

-> Going both ways in R <- Latex, Beamer, Python, Beauty Simple animation and saving movies in Python
Simple animation and saving movies in Python
2009-01-26 · via Comments for all models are wrong

January 26, 2009

Having battled through this in Matlab, doing it the Python way is extremely easy on any platform. One makes use of Mencoder, which we can just call using os.system. Here’s an idiom, culled mostly from the scipy wiki:

from pylab import *
import os
# set the interactive mode of pylab to ON
ion()
# opens a new figure to plot into
fig_hndl = figure()
# make an empty list into which we'll append
# the filenames of the PNGs that compose each
# frame.
files=[]	
# filename for the name of the resulting movie
filename = 'animation'
number_of_frames = 100
for t in range(number_of_frames):
	# draw the frame
	imshow(rand(100,100))
	# form a filename
	fname = '_tmp%03d.png'%t
	# save the frame
	savefig(fname)
	# append the filename to the list
	files.append(fname)
# call mencoder	
os.system("mencoder 'mf://_tmp*.png' -mf type=png:fps=10 
	-ovc lavc -lavcopts vcodec=wmv2 -oac copy 
	-o " + filename + ".mpg")
# cleanup
for fname in files: os.remove(fname)

Depending on how complicated the plot is, this can take anywhere from a while to ages and ages. It’s good quality, though! You can muck about with the frame rate in the call to encoder where it says fps=10. Change that 10 to something else (30 seems popular!). Also there are probably better codecs than wmv2!

Having written this, it strikes me we could probably do all this in Matlab in the same way, rather than trying to make movies using Matlab’s builtins…