uyamae2007-06-12

python で線とか簡単に引けたらアニメーションデータを視覚化するときに
楽かもしれんなぁと思ってたら、 visual なるモジュールが存在し、
さらに graph というまんまグラフを描くためのものまでありました。
ひゃーこれはゆかい。

0.374146 0.483459 -0.530334 0.587280
0.374163 0.483528 -0.530299 0.587262
0.374207 0.483702 -0.530190 0.587203
0.374272 0.483961 -0.530022 0.587111
0.374352 0.484281 -0.529811 0.586997

こんな感じのテキストを以下のスクリプトでプロットしてみました。

# coding:sjis
import fileinput as fi # 開くファイルは起動オプションで指定
# グラフのインポート
import visual as vs
import visual.graph as vg
# カーブの初期化
xcurve = vg.gcurve(color=vs.color.red)
ycurve = vg.gcurve(color=vs.color.green)
zcurve = vg.gcurve(color=vs.color.blue)
wcurve = vg.gcurve(color=vs.color.yellow)
curves = [xcurve, ycurve, zcurve, wcurve]
# アニメーション時間
frame = 0
# while ファイルからの入力が残っていたら
for line in fi.input():
  # 行末の改行を削除し、空白文字で分割
  elems = line.split("\n")[0].split(" ")
  # カーブのインデックス
  curve_index = 0
  # for 各要素
  for e in elems:
    # プロット
    curves[curve_index].plot(pos=(frame, float(e)))
    curve_index += 1
  # アニメーション時間を進める
  frame += 0.5

グラフのメモリの調整とか案外めんどいので自動でやってくれるのは超ステキです。