title: Use gnuplot via python (python-gnuplot) to plot all files at once
body: <p>The requirement is to use a python script to plot two columns from all specific .csv files in a directory at once. This works great:</p>
  
  <pre><code>plot_str = 'plot for [i=0:*] file=sprintf("cpu%03d.csv",i) title_s=sprintf("cpu%03d",i) file using 16:7 pt 7 ps 1 title title_s'
  g_plot.__call__(plot_str)
  </code></pre>
  
  <p>Where <code>g_plot</code> is:</p>
  
  <pre><code>g_plot = Gnuplot.Gnuplot(persist=1)
  </code></pre>
  
  <p>However, additionally, sometimes a cpu mask has been involved in previous steps, resulting in non-sequential file names. For example:</p>
  
  <pre><code>$ ls -1 cpu???.csv
  cpu001.csv
  cpu003.csv
  cpu005.csv
  cpu007.csv
  </code></pre>
  
  <p>In which case the above command aborts upon the first missing file in the sequence.<BR>
  I have tried several methods, most obtained from here in stackoverflow. My second to latest attempt (including debug flags and such):</p>
  
  <pre><code>#    the following commands are the current experiment.
      file_list = subprocess.check_output('ls -1 cpu???.csv',shell=True)
      title_list = subprocess.check_output('ls -1 cpu???.csv | sed -e \'s/.csv//\'',shell=True)
  
      plot_str = "plot for [i=1:words(file_list)] 'word(file_list,i)' using 16:7 pt 7 ps 1 title word(title_list,i)"
  
      print('flag 1')
      print(file_list)
      print('flag 2')
      print(title_list)
      print('flag 3')
      print(plot_str)
  
      g_plot('file_list = {0}'.format(file_list))
      g_plot('title_list = {0}'.format(title_list))
  #    g_plot.__call__('file_list = "{0}"'.format(file_list))
  #    g_plot('print file_list')
  #    g_plot.__call__('title_list = "{0}"'.format(title_list))
  #    g_plot('print title_list')
  
      g_plot.__call__(plot_str)
  </code></pre>
  
  <p>Resulting in:</p>
  
  <pre><code>flag 1
  cpu001.csv
  cpu003.csv
  cpu005.csv
  cpu007.csv
  
  flag 2
  cpu001
  cpu003
  cpu005
  cpu007
  
  flag 3
  plot for [i=1:words(file_list)] 'word(file_list,i)' using 16:7 pt 7 ps 1 title word(title_list,i)
           line 0: undefined variable: cpu001
  gnuplot&gt; cpu003.csv
           ^
           line 0: invalid command
  gnuplot&gt; cpu005.csv
           ^
           line 0: invalid command
  gnuplot&gt; cpu007.csv
           ^
           line 0: invalid command
           line 0: undefined variable: cpu001
  gnuplot&gt; cpu003
           ^
           line 0: invalid command
  gnuplot&gt; cpu005
           ^
           line 0: invalid command
  gnuplot&gt; cpu007
           ^
           line 0: invalid command
           line 0: undefined variable: file_list
  </code></pre>
  
  <p>My latest attempt:</p>
  
  <pre><code>#    the following commands are the current experiment.
      file_list = subprocess.check_output('ls -1 cpu???.csv',shell=True).splitlines()
      title_list = subprocess.check_output('ls -1 cpu???.csv | sed -e \'s/.csv//\'',shell=True).splitlines()
  #    file_list = file_list.splitlines()
  
      plot_str = "plot for [i=1:words(file_list)] 'word(file_list,i)' using 16:7 pt 7 ps 1 title word(title_list,i)"
  #    plot_str = 'plot for [i=1:words(file_list)] file=word(file_list,i) title_s=word(title_list,i) file using 16:7 pt 7 ps 1 title title_s'
  
      print('flag 1')
      print(file_list)
      print('flag 2')
      print(title_list)
      print('flag 3')
      print(plot_str)
  
      g_plot('file_list = {}'.format(file_list))
      g_plot('title_list = {}'.format(title_list))
  #    g_plot.__call__('file_list = "{0}"'.format(file_list))
  #    g_plot('print file_list')
  #    g_plot.__call__('title_list = "{0}"'.format(title_list))
  #    g_plot('print title_list')
  
      g_plot.__call__(plot_str)
  </code></pre>
  
  <p>Resulting in:</p>
  
  <pre><code>flag 1
  ['cpu001.csv', 'cpu003.csv', 'cpu005.csv', 'cpu007.csv']
  flag 2
  ['cpu001', 'cpu003', 'cpu005', 'cpu007']
  flag 3
  plot for [i=1:words(file_list)] 'word(file_list,i)' using 16:7 pt 7 ps 1 title word(title_list,i)
  gnuplot&gt; file_list = ['cpu001.csv', 'cpu003.csv', 'cpu005.csv', 'cpu007.csv']
                       ^
           line 0: invalid expression
  gnuplot&gt; title_list = ['cpu001', 'cpu003', 'cpu005', 'cpu007']
                        ^
           line 0: invalid expression
           line 0: undefined variable: file_list
  </code></pre>
  
  <p>EDIT: Attempt to use the answer from Christoph resulted in:</p>
  
  <pre><code>gnuplot&gt; cpu003.csv
           ^
           line 0: invalid command
  gnuplot&gt; cpu005.csv
           ^
           line 0: invalid command
  gnuplot&gt; cpu007.csv
           ^
           line 0: invalid command
  gnuplot&gt; ""
           ^
           line 0: invalid command
  gnuplot&gt; cpu003
           ^
           line 0: invalid command
  gnuplot&gt; cpu005
           ^
           line 0: invalid command
  gnuplot&gt; cpu007
           ^
           line 0: invalid command
  gnuplot&gt; ""
           ^
           line 0: invalid command
  </code></pre>
  
  <p>Other notes:</p>
  
  <ul>
  <li>It is desirable that this work with python version 2.7 and up. i.e. I
  tried stuff using "run" from subprocess, but version 2.7 doesn't have
  it.</li>
  <li>The number of CPUs can be quite high, over 100. A small number is
  being used to test with, for now.</li>
  <li>While the same python script does know the CPU mask from earlier
  steps, the preference is not to rely on it here, because there may be
  other reasons the related .csv file is missing.</li>
  </ul>
  
  <p><B>Question:</B> How can I pass the file list and title list strings from python to gnuplot so that they can be plotted?<BR>
  Even an array of integers, i.e. 1,3,5,7 in the above example, would probably be good enough, as the file names and titles could be re-constructed on the gnuplot side.</p>