gnuplot-py无法使用日期时间轴构建图表

时间:2012-02-28 20:12:15

标签: python gnuplot python-2.7

我使用gnuplot-py,我想用日期时间轴绘制图表。我自己使用gnuplot并且工作正常。 这是代码示例:

   import Gnuplot

   out_file = 'cpu.png'
   out_file_str = 'set out "'+out_file+'"'

   data = (('10-02-2012 18:00:36', '3.547'), ('10-02-2012 18:01:06', '3.962'), ('10-02-2012 18:04:06', '8.071'), ('10-02-2012 18:35:36', '3.513'), ('10-02-2012 18:47:06', '3.869'), ('10-02-2012 18:51:06', '3.988'), ('10-02-2012 18:56:06', '3.869'), ('10-02-2012 18:56:36', '3.811'), ('10-02-2012 18:59:36', '4.01'))
   data1 = []
   for tup in data:
     tmp = []
     tmp.append(tup[0])
     tmp.append(tup[1])
     data1.append(tmp)
   ytics = 10
   start_range = '"10-02-2012 18:00:00"'
   end_range = '"10-02-2012 19:00:00"'
   xrange = ( start_range, end_range)
   yrange = ( 0, 50 )

   chart = Gnuplot.Gnuplot()
   set_term = 'set terminal png truecolor size 780,464'
   chart(set_term)
   chart(out_file_str)

   chart('set xdata time')
   set_timefmt = 'set timefmt "%d-%m-%Y %H:%M:%S"'
   chart(set_timefmt)
   set_xformat = 'set format x "%H:%M\\n%d.%m"'
   chart(set_xformat)
   set_yformat = 'set format y "%.0f"'
   chart(set_yformat)
   set_yticformat = 'set ytics format "%.0f"'
   chart(set_yticformat)
   chart('set ytics out nomirror')
   chart('set grid xtics ytics mxtics mytics')
   chart('set xtics axis out scale 1.0,0.1 nomirror')
   chart('set key out horiz')
   chart('set key center')
   chart('set key bmargin')

   set_ytics = 'set ytics ' + str(ytics)
   chart(set_ytics)
   chart.set_range('xrange', xrange)
   chart(out_file_str)
   chart.plot(data1)

并返回此错误:

    Traceback (most recent call last):
      File "./test-gnuplot2.py", line 61, in <module>
        chart.plot(data1)
      File "/usr/lib/python2.7/site-packages/Gnuplot/_Gnuplot.py", line 284, in plot
        self._add_to_queue(items)
      File "/usr/lib/python2.7/site-packages/Gnuplot/_Gnuplot.py", line 254, in _add_to_queue
        self.itemlist.append(PlotItems.Data(item))
      File "/usr/lib/python2.7/site-packages/Gnuplot/PlotItems.py", line 549, in Data
        data = utils.float_array(data[0])
      File "/usr/lib/python2.7/site-packages/Gnuplot/utils.py", line 33, in float_array
        return numpy.asarray(m, numpy.float32)
      File "/usr/lib/python2.7/site-packages/numpy/core/numeric.py", line 235, in asarray
        return array(a, dtype, copy=False, order=order)
    ValueError: invalid literal for float(): 10-02-2012 18:00:36

我做错了什么? 对不起,我是python的新手。

1 个答案:

答案 0 :(得分:0)

这是你的意思:

for d in data1:
    chart.plot(d[0],d[1])