python gnuplot.plot 命令在字符串输入命令上失败

发布于 2024-10-10 05:42:21 字数 4013 浏览 0 评论 0原文

例如,在 gnuplot 命令行中,您可以执行

gnuplot>使用 1:2 标题“数据 A”绘制“fileA.dat”,\ 使用 python gnuplot.py使用 1:3 标题“data B”的“fileB.dat”

,以下函数工作正常:

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)

但是,当我执行 pythonic 操作并将其分解为两个函数时:


def read_data(): #与上面相同的文件/数据进行修改

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 试图绘制字符串本身,而不是像第一种情况那样解释它。

这是怎么回事?有什么办法解决这个问题吗?

in the gnuplot command line you can, for instance, do

gnuplot> plot "fileA.dat" using 1:2 title 'data A', \
"fileB.dat" using 1:3 title 'data B'

using python gnuplot.py, the following function works fine:

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)

HOWEVER, when I do the pythonic thing and break this into two functions:


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)

this fails in any number of interesting ways, apparently because gnuplot is trying to plot the string itself rather than interpret it as it does in the first case.

what's going on here? is there any way around this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明白了:

两个函数都没有执行 file.close (坏!坏!),这在单个函数的范围内是可以的 - 文件描述符仍然有效。

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

包含在 read_data() 中时,一切都会更好。

got it:

neither function does a file.close (bad! bad!), which is OK within the scope of a single function- the file descriptor is still valid.

when

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

is included in read_data() everything is better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文