开发者

Python subprocess: how to pipe to gnuplot a command to plot a variable?

I am not trying to do anything fancy here. I am trying to automate plotting some experiment data in the following script:

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() )

However filename is taken liter开发者_StackOverflow中文版ally. I get the following error from gnuplot:

line 0: warning: Skipping unreadable file "" %filename "" line 0: No data in plot

I have already parsed filename from sys.argv and made sure it is correct and safe and now I need to tell gnuplot to plot whatever name the filename is set to. I've tried using escape characters, removing the ambersand but I'm obviously using the wrong syntax.

Can someone please help ?

EDIT:

Thank's to agf I've solved the python formating issue:

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 )

However now I have an issue with gnuplot. Normally when directly using gnuplot, i would type:

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"

However, sending these commands through python to gnuplot seems to cause errors:

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

I'm betting it has to do with the ,\ and newline character ?


You want

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

See http://docs.python.org/library/string.html#formatstrings for new style formatting and http://docs.python.org/library/stdtypes.html#string-formatting for old style formatting, which looks like what you were trying to do.

Edit 2: I like part of wiso's suggestion too. Save the gnuplot commands in a file (with format codes for the variables you need to drop in), then just read that file in with Python and format it. This separates the gnuplot stuff and you don't need to worry about quoting or line endings.

If you want to send a lot of commands, I'd try writing putting all the strings in a list (preferably by reading them from a file with file.readlines()) then:

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

So it gets sent the lines one at a time but you don't have to hard code a certain number.


As agf said the problem is how you format your string. Alternatively you can concatenate the string using + operator, even if formatting is usually better.

In general I think you should separate the gnuplot code to the python code.

In the gnuplot code plot.gp put how the data should be draw. Use your python script to generate the data and write them in a tabual format into a file, for example in a temp file. Then run your gnuplot script reading from the file, see for example here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜