【发布时间】:2011-07-26 22:42:27
【问题描述】:
我不想在这里做任何花哨的事情。我正在尝试在以下脚本中自动绘制一些实验数据:
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 行:警告:跳过不可读的文件 "" %filename "" 第 0 行:图中没有数据
我已经从 sys.argv 解析了文件名,并确保它是正确和安全的,现在我需要告诉 gnuplot 绘制文件名设置的任何名称。 我尝试使用转义字符,删除了 ambersand 但我显然使用了错误的语法。
有人可以帮忙吗?
编辑:
感谢 agf 我已经解决了 python 格式化问题:
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"
但是,通过 python 将这些命令发送到 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
我打赌它与 ,\ 和换行符有关?
【问题讨论】:
-
它还没有被维护,但请尝试在这里查看:gnuplot-py.sourceforge.net
标签: python escaping subprocess string-formatting gnuplot