2011-07-26 43 views
1

我不想在這裏做任何事。我試圖在下面的腳本中自動繪製一些實驗數據:Python子流程:如何管道到gnuplot命令來繪製一個變量?

print "processing: ", filename 

gnuplot = Popen(gnuplot_bin,stdin = PIPE).stdin 

if state_plot: 

     gnuplot.write("set term x11\n".encode()) 
     gnuplot.write("set term png size 1920,1010 \n".encode()) 
     gnuplot.write("set output \"acceleration.png\" \n".encode()) 
     gnuplot.write("set xlabel \"timesteps\" \n".encode()) 
     gnuplot.write("set ylabel \"acceleration\" \n".encode()) 
     gnuplot.write("plot " %filename " using 1 with lines lt -1 lw 0.5 title 'X axis' \n " .encode()) 
     gnuplot.write(" " %filename " using 2 with lines lt 1 lw 0.5 title 'Y axis' \n " .encode()) 
     gnuplot.write(" " %filename " using 3 with lines lt 2 lw 0.5 title 'Z axis' \n " .encode()) 

但是,文件名是從字面上理解的。我從gnuplot的出現以下錯誤:

線0:警告:跳過不可讀文件「」%文件名「」 線0:劇情

我已經從SYS解析的文件名沒有任何數據。 argv並確保它是正確和安全的,現在我需要告訴gnuplot來繪製文件名被設置爲的任何名稱。 我試過使用轉義字符,刪除ambersand,但我顯然使用了錯誤的語法。

有人可以幫忙嗎?

編輯:

感謝對AGF我已經解決了蟒蛇格式化問題:

gnuplot.write("plot \"%s\" using 1 with lines lt -1 lw 0.5 title 'X axis' ,\ \n " %filename) 
     gnuplot.write("\"%s\" using 2 with lines lt 1 lw 0.5 title 'Y axis' ,\ \n " %filename) 
     gnuplot.write("\"%s\" using 3 with lines lt 2 lw 0.5 title 'Z axis' \n " %filename) 

但是現在我有gnuplot的一個問題。通常情況下,直接使用gnuplot的時候,我會鍵入:

gnuplot> plot "state_log_6548032.data" using 4 with lines lt -1 lw 0.5 title "X axis" ,\ 
>"state_log_6548032.data" using 5 with lines lt 1 lw 0.5 title "Y axis" ,\ 
>"state_log_6548032.data" using 6 with lines lt 2 lw 0.5 title "Z axis" 

然而,通過蟒蛇發送這些命令的gnuplot似乎會導致錯誤:

gnuplot> plot "state_log_6548032.data" using 1 with lines lt -1 lw 0.5 title 'X axis' ,\ 
                        ^
     line 0: invalid character \ 

gnuplot> "state_log_6548032.data" using 2 with lines lt 1 lw 0.5 title 'Y axis' ,\ 
                       ^
     line 0: invalid character \ 

gnuplot> "state_log_6548032.data" using 3 with lines lt 2 lw 0.5 title 'Z axis' 
     ^
     line 0: invalid command 

我打賭它與做的,\和換行符?

+2

這不是仍然編程和維持,但要儘量看看這裏:http://gnuplot-py.sourceforge.net/ –

回答

5

你想

gnuplot.write("plot %s using 1 with lines lt -1 lw 0.5 title 'X axis' \n " % filename1) 

舊樣式格式,它看起來像你試圖做見http://docs.python.org/library/string.html#formatstrings新樣式格式和http://docs.python.org/library/stdtypes.html#string-formatting

編輯2:我也喜歡wiso的建議的一部分。將gnuplot命令保存在一個文件中(使用需要放入的變量的格式代碼),然後用Python讀取該文件並對其進行格式化。這分開了gnuplot的東西,你不必擔心引用或行結束。

如果你想發送大量的命令,我想嘗試寫投入列表中的所有字符串(最好由具有file.readlines()文件中讀取它們),則:

for line in lines: 
    gnuplot.write(lines) 
    gnuplot.flush() 

因此它的發一行一行,但你不必硬編碼一個特定的數字。

+0

你不會也正好知道如何發送一下子GNUPLOT多行?在gnuplot中,我通常會發送每個數據,並且它的每行參數以\結尾,但這似乎不起作用。 –

+0

嘗試使用隱式字符串連接。 – agf

+0

謝謝你的幫助,可惜這也沒有幫助。問題在於gnuplot方面,而不是python。 –

1

由於agf說問題是你如何格式化你的字符串。或者,您可以使用+運算符連接字符串,即使格式通常更好。

一般來說,我認爲你應該分開gnuplot代碼到Python代碼。

在gnuplot代碼plot.gp中提出應該如何繪製數據。使用您的python腳本生成數據並將它們以表格格式寫入文件中,例如在temp file中。然後運行你的gnuplot腳本從文件中讀取,例如參見here

+0

或者只是讓Python從文件中讀取gnuplot指令並使用字符串格式化來放入變量。這將事物分開,而不必寫出數據並單獨運行gnuplot腳本。 – agf

+0

謝謝你的建議,我會研究它。可悲的是我正在尋找一個快速和骯髒的解決方案,但看起來這畢竟不是那麼簡單。 –