cmd = ''
cmd += "set terminal png\n"
cmd += "set output 'rate_avg_" +name+ ".png'\n"
cmd += "p '" +name+ "_avg.log' u " + str(xyconv((1,2),3)+1) + "w lp t '1->2(avg)',"
cmd += "'" +name+ "_inst.log' u " + str(xyconv((1,2),3)+1) + "w lp t '1->2(inst)',"
cmd += "'" +name+ "_avg.log' u " + str(xyconv((2,3),3)+1) + "w lp t '2->3(avg)',"
cmd += "'" +name+ "_inst.log' u " + str(xyconv((2,3),3)+1) + "w lp t '2->3(inst)',"
cmd += "'" +name+ "_avg.log' u " + str(xyconv((3,1),3)+1) + "w lp t '3->1(avg)',"
cmd += "'" +name+ "_inst.log' u " + str(xyconv((3,1),3)+1) + "w lp t '3->1(inst)'\n"
gp=os.popen('gnuplot', 'w')
gp.write(cmd)
gp.flush()
我只会用gnuplot来画图,python有一个gunuplot的库(gnuplot.py)可以对在python中生成的数据来画图,但是我的图像是存成trace文件的,要用这个库要读出trace反而麻烦了。于是我就看了看gnuplot.py的源代码,原来他调用gnuplot的方式是popen。
以上的程序就是用了gnuplot.py的方法,然后程序中画图的一段。如果你要是用过gnuplot大概一看就明白了。
后来想到其实也有个更简单的方法,就是写好一个gnuplot的script然后每次用os.system来调用也是可以的,但这样灵活性就差一点。当然也可以用python来生成gnuplot的script然后再用os.system来调用,方法反正很多的了!