title: Python subprocess: how to pipe to gnuplot a command to plot a variable?
body: <p>I am not trying to do anything fancy here. I am trying to automate plotting some experiment data in the following script:</p>
  
  <pre><code>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() )
  </code></pre>
  
  <p>However filename is taken literally. I get the following error from gnuplot:</p>
  
  <blockquote>
    <p>line 0: warning: Skipping unreadable file "" %filename ""
             line 0: No data in plot</p>
  </blockquote>
  
  <p>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.</p>
  
  <p>Can someone please help ?</p>
  
  <p>EDIT:</p>
  
  <p>Thank's to agf I've solved the python formating issue:</p>
  
  <pre><code>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 )
  </code></pre>
  
  <p>However now I have an issue with gnuplot. Normally when directly using gnuplot, i would type:</p>
  
  <pre><code>gnuplot&gt; plot "state_log_6548032.data" using 4 with lines lt -1 lw 0.5 title "X axis" ,\
  &gt;"state_log_6548032.data" using 5 with lines lt 1 lw 0.5 title "Y axis" ,\
  &gt;"state_log_6548032.data" using 6 with lines lt 2 lw 0.5 title "Z axis"
  </code></pre>
  
  <p>However, sending these commands through python to gnuplot seems to cause errors: </p>
  
  <pre><code>gnuplot&gt; plot "state_log_6548032.data" using 1 with lines lt -1 lw 0.5 title 'X axis' ,\ 
                                                                                         ^
           line 0: invalid character \
  
  gnuplot&gt; "state_log_6548032.data" using 2 with lines lt 1 lw 0.5 title 'Y axis' ,\ 
                                                                                   ^
           line 0: invalid character \
  
  gnuplot&gt; "state_log_6548032.data" using 3 with lines lt 2 lw 0.5 title 'Z axis' 
           ^
           line 0: invalid command
  </code></pre>
  
  <p>I'm betting it has to do with the ,\ and newline character ?</p>