2011-01-05 50 views
0

在gnuplot的命令行可以,例如,做蟒gnuplot.plot命令串上輸入命令失敗

gnuplot的>情節「fileA.dat」使用1:2標題「數據A」,\ 「 fileB.dat」用1:3標題‘數據B’使用python gnuplot.py

,以下功能正常工作:

def create_plot(): 
    ... 
    for target in TARGET_LIST: 
     for protocol in PROTOCOL_LIST: 

      input_file_name = "%s/%s.db" % (DATA_DIR, target) 
      shortname = input_file_name.split('/')[-1] 
      shortname = shortname.rstrip('.db') 

      input_list = read_lines(input_file_name) 

      write_plot_file_name = '%s/%s.%s.write.out' % (DATA_DIR, shortname, protocol) 
      write_plot_file = open(write_plot_file_name, 'w') 

      read_plot_file_name = '%s/%s.%s.read.out' % (DATA_DIR, shortname, protocol) 
      read_plot_file = open(read_plot_file_name, 'w') 

      ping_plot_file_name = '%s/%s.ping.out' % (DATA_DIR, shortname) 
      ping_plot_file = open(ping_plot_file_name, 'w') 

      for line in input_list[ limit: ]: 

       line = line.rstrip("\n") 
       line_protocol, line_verb, delta, timestamp = line.split('|') 

       if line_protocol == protocol: 
        if line_verb == 'write': 
         write_plot_file.write("%s,%s\n" % (timestamp, delta)) 
        else: 
         read_plot_file.write("%s,%s\n" % (timestamp, delta)) 
       elif line_protocol == 'ping': 
        ping_plot_file.write("%s,%s\n" % (timestamp, delta)) 

      #gnuplot stuff 

      png_file = "%s/%s.%s.png" % (PLOT_DIR, shortname, protocol) 

      title = '%s history for %s ' % (protocol, shortname) 

      gnuplot = Gnuplot.Gnuplot() 
      gnuplot.title(title) 
      gnuplot('set style data linespoints') 
      gnuplot('set ylabel "seconds"') 
      gnuplot('set xlabel "month:day:hour:minute:seconds:millisecond"') 
      gnuplot('set xdata time') 
      gnuplot('set timefmt "%s"') 
      gnuplot('set format x "%m:%d:%H:%M:%S:%MS"') 
      gnuplot('set xtics nomirror rotate by -90') 
      gnuplot('set datafile separator ","') 
      gnuplot('set autoscale') 
      gnuplot('set grid xtics ytics') 
      gnuplot('set terminal png size 900,899') 
      gnuplot('set output "%s"' % (png_file)) 

      cmd = ' "%s" using 1:2 axes x1y1 title "write" with lines, \ 
        "%s" using 1:2 axes x1y1 title "read" with lines, \ 
        "%s" using 1:2 axes x1y2 title "ping" with lines' % \ 
        (write_plot_file_name, read_plot_file_name, ping_plot_file_name) 

      try: 
       gnuplot.plot(cmd) 
      except Error as why: 
       print "gnuplot choked: %s" % (why) 

然而,當我做了Python的事情,分成兩個功能,這:


def read_data(): #do the same file/data munging above

png_file = "%s/%s.%s.png" % (PLOT_DIR, shortname, protocol) 

title = '%s history for %s ' % (protocol, shortname) 

cmd = ' "%s" using 1:2 axes x1y1 title "write" with lines, \ 
    "%s" using 1:2 axes x1y1 title "read" with lines, \ 
    "%s" using 1:2 axes x1y2 title "ping" with lines' % \ 
    (write_plot_file_name, read_plot_file_name, ping_plot_file_name) 

def create_plot(png_file, title, cmd): 
#call the gnuplot 'set' strings as above 
gnuplot = Gnuplot.Gnuplot() 
gnuplot.title(title) 
gnuplot('set style data linespoints') 
gnuplot('set ylabel "seconds"') 
gnuplot('set xlabel "month:day:hour:minute:seconds:millisecond"') 
gnuplot('set xdata time') 
gnuplot('set timefmt "%s"') 
gnuplot('set format x "%m:%d:%H:%M:%S:%MS"') 
gnuplot('set xtics nomirror rotate by -90') 
gnuplot('set datafile separator ","') 
gnuplot('set autoscale') 
gnuplot('set grid xtics ytics') 
gnuplot('set terminal png size 900,899') 
gnuplot('set output "%s"' % (png_file))`) 

gnuplot.plot(cmd) 這個失敗的方式有很多,顯然是因爲gnuplot試圖繪製字符串本身而不是像第一種情況那樣解釋它。

這是怎麼回事?有沒有辦法解決?

+0

你能把這個確切的錯誤信息?可能所有你需要做的就是轉換爲適當的數據類型。 – Navi 2011-01-05 21:11:30

+0

失敗在gnuplot。如上面編碼:第0行:警告:跳過沒有有效點的數據文件 ...或: 第0行:所有點y值未定義! ...如果格式化cmd字符串:gnuplot.plot('%s'%(cmd))yield:line 0:所有點y值undefined! ...或...第0行:x範圍無效 ...或第0行:所有點y值未定義! – user498844 2011-01-06 00:09:01

+0

當你移動它們時,我會說你的設置命令出了問題。你可以發佈「設置蜇如上」 錯誤消息表明gnuplot打開文件,但無法讀取它 - 與http://stackoverflow.com/questions/4164052/gnuplot-v4-4-problem-plotting -using-timeseries-x-axis/4164684#4164684 – Tom 2011-01-06 10:52:58

回答

0

得到它:

兩個函數做了file.close(壞人壞事!),這是一個單一功能 - 文件描述符的範圍內確定是否仍然有效。

write_plot_file.close() 
read_plot_file.close() 
ping_plot_file.close() 

包括在的read_data()事情就會變得更好。