複数のグラフ作成

いくつかデータが存在してグラフを複数出したいときについて考えてみる。
例えばこんなデータとか

#time   sim-vx    sim-vy   test-vx   test-vy
0.000, -0.01991,  0.46010, -0.01983,  0.46400
0.005, -0.01992,  0.46010, -0.01985,  0.46000
0.010, -0.01994,  0.46000, -0.02000,  0.46200
0.015, -0.01997,  0.46000, -0.01956,  0.46060
0.020, -0.01999,  0.45990, -0.01986,  0.45740
....................

sim-*はシミュレーションで求めた値でtest-*は実測値とかいうシチュエーション。

グラフ領域2分割

複数のグラフを描くときは

subplot(xyz)

で分割する。xyzにはそれぞれ数字が入ってxは上下の分割数・yは左右の分割数・zはインデックスを表す・・・と思う。

import pylab
fin = open('result.dat', 'r')
time = []
sim_vx = []
sim_vy = []
test_vx = []
test_vy = []
line = fin.readline()
for line in fin:
	dat = line.rstrip('\n') #改行コードを取り除く
	dat = dat.split(',') #カンマでデータを区切る
	time.append(float(dat[0]))
	sim_vx.append(float(dat[1]))
	sim_vy.append(float(dat[2]))
	test_vx.append(float(dat[3]))
	test_vy.append(float(dat[4]))
fin.close()

pylab.subplot(211) #グラフエリアを上下に分割した上
pylab.plot(time, sim_vx, "r-", label="simulation")
pylab.plot(time, test_vx, "b-.", label="test")
pylab.legend() #グラフの凡例を表示
pylab.ylabel("x-axis velocity [m/s]")
pylab.title("simulation and execution result")

pylab.subplot(212) #グラフエリアを上下に分割した下
pylab.plot(time, sim_vy, "m-", label="simulation")
pylab.plot(time, test_vy, "g-.", label="test")
pylab.legend() #グラフの凡例を表示
pylab.ylabel("y-axis velocity [m/s]")
pylab.xlabel("Time [s]")
pylab.show()


実行結果見たけどシミュレーションと実測値の違いが見えねぇ。
ちなみにplot()内の"r-"とか"b-."とかは線の色や種類を表す。
他の色とか線種はドキュメント見てっちょ。pyplot — Matplotlib 2.0.2 documentation

グラフ領域4分割

ちょっとグラフエリアを4分割して一部拡大してみる。

import pylab
fin = open('result.dat', 'r')
time = []
sim_vx = []
sim_vy = []
test_vx = []
test_vy = []
line = fin.readline()
for line in fin:
	dat = line.rstrip('\n') #改行コードを取り除く
	dat = dat.split(',') #カンマでデータを区切る
	time.append(float(dat[0]))
	sim_vx.append(float(dat[1]))
	sim_vy.append(float(dat[2]))
	test_vx.append(float(dat[3]))
	test_vy.append(float(dat[4]))
fin.close()

pylab.subplot(221)
pylab.plot(time, sim_vx, "r-", label="simulation")
pylab.plot(time, test_vx, "b-.", label="test") #プロットデータ指定
pylab.legend()
pylab.title("simulation and execution result")
pylab.ylabel("x-axis velocity [m/s]")

pylab.subplot(222)
pylab.plot(time[160:180], sim_vx[160:180], "or-", label="simulation")
pylab.plot(time[160:180], test_vx[160:180], "^b-.", label="test") #プロットデータ指定
pylab.legend()
pylab.title("simulation and execution result")

pylab.subplot(223)
pylab.plot(time, sim_vy, "m-", label="simulation")
pylab.plot(time, test_vy, "g-.", label="test") #プロットデータ指定
pylab.legend()
pylab.xlabel("Time [s]")
pylab.ylabel("y-axis velocity [m/s]")

pylab.subplot(224)
pylab.plot(time[200:220], sim_vy[200:220], "vm-", label="simulation")
pylab.plot(time[200:220], test_vy[200:220], "Dg-.", label="test") #プロットデータ指定
pylab.legend()
pylab.xlabel("Time [s]")

pylab.show()


まぁ4分割できましたねーくらいのレベルか。
だいぶ力任せに作った感がある。matplotlibのサイト内にあるサンプルとか読んで勉強しないとなー。
今回ラベルとか軸の統一とかめんどいから放置したけどこれもどーにかせないかん。
次は軸とかラベルあたりについて調べるかね。