2011-07-26 38 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

谢谢你的建议,我会研究它。可悲的是我正在寻找一个快速和肮脏的解决方案,但看起来这毕竟不是那么简单。 –