2011-07-27 47 views
4

通常使用交互式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.write("plot \"%s\" using 1 with lines lt -1 lw 0.5 title 'X axis' ,\ \n" %filename) 
gnuplot.write("plot \"%s\" using 2 with lines lt 1 lw 0.5 title 'Y axis' ,\ \n" %filename) 
gnuplot.write("plot \"%s\" using 3 with lines lt 2 lw 0.5 title 'Z axis' \n" %filename) 

我得到以下錯誤:

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

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

我花了很多時間試圖找出pytho是否有問題n,但我發現這是gnuplot的一個問題,從控制檯調用時出於某種原因使用轉義字符,但在我的情況下不需要。但是我的問題依然存在我怎樣才能通過管道指令來從python子進程繪製連續行中的^^以上的數據,或者通過從python創建一個gnu文件並調用gnuplot子進程來使用該文件?

編輯:

爲了任何人誰可能永遠陷在這個簡單的小東西:由民間好看誰保持這個社會活着解釋如下,巨蟒當您使用「\」轉義「\」。 因此,解決辦法很乾脆:

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) 
+0

你有沒有試過[gnuplot模塊](http://gnuplot-py.sourceforge.net/)?另外,如果我可以推薦使用[matplotlib](http://matplotlib.sourceforge.net/)來進行繪圖。 – Keith

+0

@Keith我有debian的python-gnuplot-1.8,但我找不到任何有關如何使用它的文檔。它似乎沒有維護。我不知道matplotlib,非常感謝你的鏈接,這看起來更像我之後的第一個地方! –

回答

4

在命令行GNUPLOT比如你貼,你逃脫換行符,以避免在你的屏幕數英里長的線,但仍送TU GNUPLOT爲單行數據。

這很酷,如果你手動輸入數據,但如果你正在以編程方式進行,你爲什麼要關心?只需發送一行,Python就不會抱怨可讀性:)。如果您對這個問題感到好奇,那是因爲Python 也使用\作爲轉義序列,所以該字符永遠不會到達gnuplot

也就是說,有python-gnuplot庫來處理所有這些骯髒的工作。去看一下!

+0

我真的不在乎我是怎麼做到的,但這是我知道在gnuplot中發送命令的唯一方法。 agf在另一個問題中告訴我,我可以創建一個tmp文件並將命令放在那裏,並將它們一個接一個地輸入到gnuplot中。我也想過在gnu文件中輸出所有命令並調用gnuplot來加載該文件。但現在我仍然無法獲得上述代碼的工作。 –

1

我認爲當你在Python做\(反彈空間),你逃避的空間,我想,當你做\然後打在gnuplot的輸入您」重新轉義換行。

你試過了嗎,最後只有,\n,python的方式來換行嗎?僅用,,(逗號空格)?只需\n

也許,如果你不得不逃離換行,這不是必要的,只是,將工作分開的命令。

+0

你好agf。我試過了,\ n和我得到:第0行:功能預計。 我試過了:\ n雖然它可以工作,但它只繪製第一行。 終於我試過了,\\\ n和它的工作:) 對不起,打擾你這一點。我現在將嘗試使用文件自動化它,如您在其他問題中所建議的那樣。 –

+0

我試圖通過'\'逃脫空間,但他們一直讓我回到地球。 – wander95

1

要將字面反斜槓提供給gnuplot,請在Python字符串中使用\\(與使用\n爲它提供文字換行符的方式相同)。 \一般會啓動轉義序列,在你的情況下你有\;對此沒有特別的意義,所以它只是被解釋爲一個空格(即反斜槓被刪除)。

您可以通過print來測試字符串的內容,而不是將它們傳遞給gnuplot.write

這就是說,機會是好的,你可以把整個事情放在一行上,而不是反饋gnuplot反斜槓和換行符。

+0

我已經得到了python-gnuplot-1.8,但我找不到有關如何使用它的任何文檔! –

1

在我的Gnuplot 4.6的情況下,和openSUSE Linux的類型和封閉撇號的順序是非常重要的:用gnuplot的開始作爲subprocess.Popen,寫作:

gnuplot.stdin.write("set format x '%d %h\\\n%H:%M'") 

...印刷換行符在單一的文字 - 線標籤,時:

gnuplot.stdin.write('set format x "%d %h\\\n%H:%M"') 

...按預期工作,沿時間x軸生成雙行標籤。