gnuplot 带参数脚本与python脚本结合

 

gnuplot是强大的绘图软件

例子:gnuplot的脚本如下:

1 reset
2 set term png
3 set output output_fname
4 plot input_fname using 1:2 w lp pt 0 title 'data'

python负责生成数据,并调用gnuplot脚本:

# -*- coding: utf-8 -*-
'''
 @Time : 2018/6/28 14:41
 @Author : [email protected]
 @Site : 
 @File : blog.py
 @Software: PyCharm
'''
import numpy as np
from subprocess import Popen
import os

preDir = os.getcwd()
env = os.environ
print(env)
print(os.get_exec_path(env))
a = np.random.standard_normal(10)
f = open(os.path.join(preDir, "data.txt"), "w")
for i in range(10):
    f.write("{} {}\n".format(i, a[i]))
f.close()

in_path = "\'"+os.path.join(preDir, "data.txt")+"\'"
out_path = "\'"+os.path.join(preDir, "data.png")+"\'"
cmd = ['gnuplot',
       '-e',
       "input_fname=" + in_path + ";output_fname=" + out_path + "",
       os.path.join(preDir, "plot.plt")
       ]
Popen(cmd)

结果:

gnuplot 带参数脚本与python脚本结合_第1张图片

你可能感兴趣的:(Python,gnuplot)