【问题标题】:python gnuplot.plot command fails on string input commandpython gnuplot.plot 命令在字符串输入命令上失败
【发布时间】:2011-01-05 20:21:00
【问题描述】:

例如,在 gnuplot 命令行中,您可以这样做

gnuplot> 使用 1:2 标题“数据 A”绘制“fileA.dat”,\ “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)

但是,当我做 pythonic 的事情并将其分解为两个函数时:


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

这里发生了什么?有没有办法解决这个问题?

【问题讨论】:

  • 你能把确切的错误信息放在这里吗?可能您需要做的就是转换为适当的数据类型。
  • 故障出现在 gnuplot 中。如上代码:第 0 行:警告:跳过没有有效点的数据文件...或:第 0 行:所有点 y 值未定义! ...如果您格式化 cmd 字符串: gnuplot.plot('%s' % (cmd)) 产生: 第 0 行:所有点 y 值未定义! ... or ... line 0: x range is invalid ... or line 0: all points y value undefined!
  • 我会说,当您移动它们时,您的 set 命令出了点问题。你能发布“如上所述设置刺痛”错误消息表明gnuplot正在打开文件但无法读取它 - 与stackoverflow.com/questions/4164052/…一样
  • aarrghg。 cmets中没有格式。我将它添加到上面的示例中

标签: python gnuplot


【解决方案1】:

知道了:

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

什么时候

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

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

【讨论】:

    猜你喜欢
    • 2018-11-25
    • 2014-04-20
    • 2020-10-07
    • 2011-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-01
    • 1970-01-01
    相关资源
    最近更新 更多