Сбой команды python gnuplot.plot для команды ввода строки - PullRequest
0 голосов
/ 05 января 2011

в командной строке gnuplot вы можете, например, сделать

gnuplot> заговор "fileA.dat" с использованием 1: 2 заголовка 'data A', \ "fileB.dat", используя заголовок 1: 3 'data 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)

ОДНАКО, когда я занимаюсь питонизмом и разбиваю его на две функции:

<br> def read_data(): #do the same file/data munging above</p> <pre><code>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 пытается построить саму строку, а не интерпретировать ее, как в первом случае.

что здесь происходит? есть ли способ обойти это?

1 Ответ

0 голосов
/ 07 января 2011

понял:

ни одна из функций не делает файл file.close (плохо! Плохо!), Что нормально в рамках одной функции - дескриптор файла все еще действителен.

когда

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

включено в read_data () все лучше.

...