通常在交互式使用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.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 \
我花了很多时间来查明是Python的问题,但我发现这是一个gnuplot的问题。在从控制台调用时,它会使用转义字符,但在我的情况下不需要。但我的问题仍然存在。如何通过从Python子进程通过管道传输指令或从Python创建一个gnu文件并调用gnuplot子进程来使用该文件来将上述数据绘制成连续的线条?
编辑:
对于任何可能陷入这个简单小事的人:正如下面解释的那样,当你使用“\”时,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 )