ファイルから数値を読み込んで2-Dプロットを作成
matplotlibを使って、グラフを描く。
ファイル(data.txt)からデータを読み込んで、プロットするときはこんな感じでできる。文字列の分割の部分をもう少しエレガントにできないか、とも思う。
from pylab import * f = open("data.txt", "r") xlist = list() ylist = list() for line in f: s = line.split(",") xx = s[0] yy = s[1][:-1] xlist.append(xx) ylist.append(yy) f.close() lines = plot(xlist, ylist, 'k') setp(lines, color='r', linewidth=2.0) xlabel('x') ylabel('y') show()
データファイル(data.txt)は次の通り。
0.0, 1.0 1.0, 2.0 2.0, 3.0 3.0, 4.0 4.0, 2.5 5.0, 1.3 6.0, 0.0 7.0, -1.2 8.0, -2.0 9.0, -3.0 10.0, -1.2 11.0, 0.03 12.0, 1.5 13.0, 2.5 14.0, 5.0 15.0, 8.0