毎度のグラフ描画スクリプト

Excel 2007でグラフを作成すると頻繁にフリーズしてどうしようもないので、Matplotlibで作図している。いつものことだ。毎回、読み込むデータの形式が微妙に異なるので、使い捨てスクリプトを書く。
今回はこれ。

import os
from pylab import *
from matplotlib.ticker import FormatStrFormatter #使っていない。

# ---------------------------------------------------
#  variables
# ---------------------------------------------------
namd = "Case1_01"    # folder name
# ---------------------------------------------------

""" fixed variables (you should not change)"""
namf = 24   # number of files for probes
point = [1,2,3,10,11,12,4,5,6,7,8,9]
prsFmt = FormatStrFormatter('%d')

for i in xrange(1,namf+1):

    """ reading data """
    time = []   # Time
    temp1 = []
    temp2 = []
    temp3 = []
    namf = 'probe.txt_' + str(i)

    file_path = os.path.join(os.getcwd(), namd, namf)
    filedscript_read = open(file_path, 'r')
    All_lines = filedscript_read.readlines()
   
    saved_file = str(i)
    filedscript_read.close()

    coord = All_lines[2][1:]
    yval = All_lines[4].split()[2]
    print 'Number=',i, namf, yval[-3:]
    if yval[-3:] == "prs":
        ylab = "Pressure [Pa]"
    elif yval[-3:] == "vel":
        ylab = "Velocity [m/s]"
#    graph_title = str(coord)
    graph_title = ylab + "  (Point #" + str(point[(i-1)%12]) +")"

    """ calculational data """
#    for NUM in xrange(5, len(All_lines)):
    for NUM in xrange(9, len(All_lines)):  #To delete initial noise
        time.append(float(All_lines[NUM].split()[0]))
        temp1.append(float(All_lines[NUM].split()[1]))
        if ylab == "Velocity [m/s]":
            temp2.append(float(All_lines[NUM].split()[2]))
            temp3.append(float(All_lines[NUM].split()[3]))

    """ making figure """
    fig = figure()
    ax = fig.add_subplot(211)
    ax.yaxis.set_major_formatter(prsFmt)
    if ylab == "Pressure [Pa]":
        ax.plot(time, temp1, 'k', color='r', linewidth=1.5)
    if ylab == "Velocity [m/s]":
        ax.plot(time, temp1, 'k', color='r', label='U', linewidth=1.5)
        ax.plot(time, temp2, 'k', color='b', label='V', linewidth=1.5)
        ax.plot(time, temp3, 'k', color='g', label='W', linewidth=1.5)
        ax.legend(loc='best')
    title(graph_title, fontsize=16)
    xlabel('Time [sec]', fontsize=16)
#    ylabel(ylab, fontsize=18)
    grid(True)

    ax.autoscale_view()
    fig.savefig(saved_file)
    clf()

これなら数十秒で複数のグラフを作成できるので、ストレスフリー。
でも図の大きさの調整とかまだうまくできないのだが。ylabelを置くと、外にはみ出るから今回はグラフの上にラベルを置いた。

毎回、スクリプトを書くのは面倒になってきたので、汎用グラフ作図ソフトでも作りたい。Excelより高速に動作するのは確実なので、重宝するだろうと思う。1日、集中できれば作れるのだが。