python gnuplotpy with datetime



我一直在尝试用gnuplot.py打印以下数据:

data = [[datetime.datetime(2013, 1, 15, 17, 45), 16.00], [datetime.datetime(2013, 1, 16, 17, 45), 15.98], [datetime.datetime(2013, 1, 17, 17, 45), 15.94]]

所以我开始这样做:

gp=Gnuplot.Gnuplot()
gp('set data style linespoints')
gp('set xdata time')
gp('set timefmt "%Y-%m-%d"')
gp('set xrange ["2012-12-20":"2013-02-12"]')
gp.plot(data)

我得到以下回应:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/dist-packages/Gnuplot/_Gnuplot.py", line 285, in plot
    self._add_to_queue(items)
  File "/usr/lib/python2.6/dist-packages/Gnuplot/_Gnuplot.py", line 255, in _add_to_queue
    self.itemlist.append(PlotItems.Data(item))
  File "/usr/lib/python2.6/dist-packages/Gnuplot/PlotItems.py", line 549, in Data
    data = utils.float_array(data[0])
  File "/usr/lib/python2.6/dist-packages/Gnuplot/utils.py", line 33, in float_array
    return numpy.asarray(m, numpy.float32)
  File "/usr/lib/python2.6/dist-packages/numpy/core/numeric.py", line 230, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence.

然后,我尝试将其转换为以下列表:

[['2013-01-15', 16.00], ['2013-01-16', 15.98], ['2013-01-17', 15.94]]

同样的结果。

有人知道问题出在哪里吗?

谢谢。

问题(我相信)在gnuplot py内部的这一行,我从回溯中提取了它:

return numpy.asarray(m, numpy.float32)

最终发生的是gnuplot py试图将日期-时间(或字符串)数据转换为numpy.float32对象。不过,如果你愿意跳过一些困难,我们可能会让它发挥作用。


在检查源时,这部分解决方案实际上可能没有必要您需要做的第一件事是将utils.py内的行更改为使用numpy.float64而不是numpy.float32,然后重新安装gnuplot-py。这样我们就可以使用某种标准的历元表示法。(或者,您可以绕过重新安装步骤,只需将其更改为回溯中引用的文件——它甚至会给您行号):

File "/usr/lib/python2.6/dist-packages/Gnuplot/utils.py", line 33, in float_array
    return numpy.asarray(m, numpy.float32)

现在第33行读取numpy.asarray(m,numpy.float64)


我们可以使用浮点数来存储历元信息(float32只是没有给出足够的精度,我认为…)

def secs_since_epoch(date_time,epoch=datetime.datetime(1970,1,1,0,0,0)):
    """
    compute seconds since a particular epoch.  The default epoch is the unix
    epoch -- e.g. Jan. 1 1970.
    """
    dt = date_time - epoch  #time elapsed since unix epoch
    return dt.days*24*3600 + dt.seconds + dt.microseconds*1e-6 #converted to seconds

您可以在日期时间对象上使用此函数,将它们从日期时间对象转换为简单(双精度)浮点。谢天谢地,Gnuplot也可以为时间对象读取这种格式:

gp = Gnuplot.Gnuplot()
gp('set data style linespoints')
gp('set xdata time')
gp('set timefmt "%s"')
xmin = secs_since_epoch(datetime.datetime(2012,12,20,0,0))
xmax = secs_since_epoch(datetime.datetime(2012,2,12,0,0))
gp('set xrange [%s:%s]' % (xmin,xmax) )
gp.plot(data)

以下脚本对我来说还可以:

import datetime
import Gnuplot
data = [[datetime.datetime(2013, 1, 15, 17, 45), 16.00],
        [datetime.datetime(2013, 1, 16, 17, 45), 15.98],
        [datetime.datetime(2013, 1, 17, 17, 45), 15.94]]
def secs_since_epoch(date_time,epoch=datetime.datetime(1970,1,1,0,0,0)):
    """
    compute seconds since a particular epoch.  The default epoch is the unix
    epoch -- e.g. Jan. 1 1970.
    """
    dt = date_time - epoch  #time elapsed since unix epoch
    return dt.days*24*3600 + dt.seconds + dt.microseconds*1e-6 #converted to seconds
dt,vals = zip(*data)
data = zip(map(secs_since_epoch,dt),vals)
gp = Gnuplot.Gnuplot(debug=True)
gp('set style data linespoints')
gp('set xdata time')
gp('set timefmt "%s"')
xmin = secs_since_epoch(datetime.datetime(2012,12,20,0,0))
xmax = secs_since_epoch(datetime.datetime(2012,2,12,0,0))
#gp('set xrange [%s:%s]' % (xmin,xmax) )
d = Gnuplot.Data(data)
d.set_option(using="1:2")
gp.plot(d)
raw_input()

相关内容

  • 没有找到相关文章

最新更新