gnuplot 帶參數指令碼與python指令碼結合

來源:互聯網
上載者:User

標籤:txt   ima   inf   分享   format   TE   blog   pre   技術分享   

因為論文需要使用gnuplot作圖,而我喜歡使用python來產生資料,所以研究了下gnuplot的帶參數的指令碼,這樣我就可以在產生資料的同時調用gnuplot,實現的方法有很多,在這裡我只講我這種。

1 reset2 set term png3 set output output_fname4 plot input_fname using 1:2 w lp pt 0 title ‘data‘

上面是一段簡單的畫折線圖的gnuplot指令碼,參數就是裡面的output_fname和input_fname,在調用該指令碼時需要在命令列中指定這兩個參數,

 1 # -*- coding: utf-8 -*- 2 ‘‘‘ 3  @Time : 2018/6/28 14:41 4  @Author : [email protected] 5  @Site :  6  @File : blog.py 7  @Software: PyCharm 8 ‘‘‘ 9 import numpy as np10 from subprocess import Popen11 import os12 13 preDir = os.getcwd()14 env = os.environ15 print(env)16 print(os.get_exec_path(env))17 a = np.random.standard_normal(10)18 f = open(os.path.join(preDir, "data.txt"), "w")19 for i in range(10):20     f.write("{} {}\n".format(i, a[i]))21 f.close()22 23 in_path = "\‘"+os.path.join(preDir, "data.txt")+"\‘"24 out_path = "\‘"+os.path.join(preDir, "data.png")+"\‘"25 cmd = [‘gnuplot‘,26        ‘-e‘,27        "input_fname=" + in_path + ";output_fname=" + out_path + "",28        os.path.join(preDir, "plot.plt")29        ]30 Popen(cmd)

第23行到30行就是指令碼的執行過程,這裡面使用了Popen來執行命令列。

效果如下:

 

gnuplot 帶參數指令碼與python指令碼結合

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.