gnuplotとGiamでアニメーション

gnuplotを使ってグラフのアニメーションを作りたい、せめて連番のファイルをまとめてグラフにしたい、ということで調べていたら次のサイトにたどりついた。
http://www.cfca.nao.ac.jp/~takedatk/COMPUTER/HowToMakeAnimation/gnuplot+perl+imagemagick/HowToMakeAnimation01.html

なるほどスクリプトを出力するスクリプトを書けばいいのねと言うことでPythonで書いてみた。まあ、そうですよね、うん。

def main():
	f = open("plot.plt", "w")
	f.write("set xrange [-2:2]\n")
	f.write("set yrange [-2:2]\n")
	f.write("set zrange [-2:2]\n")
	f.write("set term png\n")
	for i in range(500):    #500はデータ数
		f.write("set output \"img/%03d.png\"\n" %i )
		f.write("splot \"%d\" with lines\n" % i)    #プロットするデータのファイル名を入れる。ここではとりあえず数字だけ。
		f.write("set output\n")
	f.close()

if __name__ == "__main__":
	main()

plot.pltの中身はこんな感じになる。

set xrange [-2:2]
set yrange [-2:2]
set zrange [-2:2]
set term png
set output "img/000.png"
splot "0" with lines
set output
set output "img/001.png"
splot "1" with lines
set output
set output "img/002.png"
splot "2" with lines
set output
set output "img/003.png"
splot "3" with lines
set output

・・・中略・・・

set output "img/497.png"
splot "497" with lines
set output
set output "img/498.png"
splot "498" with lines
set output
set output "img/499.png"
splot "499" with lines
set output

このスクリプトgnuplotで使うとカレントディレクトリにあるimgというディレクトリの下に画像が出力される。実行する前にimgという名前のディレクトリを作っておかないとたぶんエラーが出ると思う。

出力された画像をgifアニメにするとこんな感じになる。作成にはGiamというフリーソフトを使った。
http://homepage3.nifty.com/furumizo/giamd.htm

ちなみにこの画像はラングフォード方程式のグラフで、パラメータaを0から1.0まで0.002刻みで増やしたときの様子を表している。
\{\frac{dx}{dt}=(z-0.7)x-3.5y\\\frac{dy}{dt}=3.5x+(z-0.7)y\\\frac{dz}{dt}=a+z-\frac{z^{3}}{3}-(x^{2}+y^{2})(1+0.25z)