Python Pyplot在轴上的unix时间戳与烛台图

时间:2014-01-05 03:53:30

标签: python datetime matplotlib

我试图通过pyplot查看有关将时间戳绘制为字符串的一些答案,但没有一个答案能够解决我的问题。

以下是我尝试对数据进行组织的示例[timestamp,open,close,max,min,avg,volume]。烛台工作正常,但我试图在x轴上添加显示日期时间。

我知道我需要将它们转换为日期时间对象以使格式化程序起作用,但我不确定使用这种类型的绘图实现此目的的最佳方法是什么。这就是我现在所拥有的:

results=[[1388893323, 24.309684210526317, 24.302, 24.309684210526317, 24.261, 24.2828875, 172.1496], [1388893203, 24.301997222222223, 24.2501, 24.301997222222223, 24.2501, 24.28035104166669, 198.16475999999997], 
[1388893083, 24.2501, 24.311, 24.311, 24.2501, 24.276614722222227, 1002.402654], 
[1388892963, 24.311, 24.311, 24.37764285714286, 24.311, 24.313886785714317, 111.5695297]]

xfmt = DateFormatter('%Y-%m-%d %H:%M:%S')

fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)
ax.xaxis.set_major_formatter(xfmt)

candlestick(ax, results, width=20)

ax.xaxis_date()
ax.autoscale_view()
plt.setp( plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')

plt.show()

产生错误我认为与不是日期时间对象有关:

  File "sma.py", line 27, in <module>
    plt.setp( plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')
  File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 2530, in get_xticklabels
    self.xaxis.get_ticklabels(minor=minor))
  File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 1109, in get_ticklabels
    return self.get_majorticklabels()
  File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 1093, in get_majorticklabels
    ticks = self.get_major_ticks()
  File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 1191, in get_major_ticks
    numticks = len(self.get_major_locator()())
  File "/usr/lib/pymodules/python2.7/matplotlib/dates.py", line 749, in __call__
    self.refresh()
  File "/usr/lib/pymodules/python2.7/matplotlib/dates.py", line 758, in refresh
    dmin, dmax = self.viewlim_to_dt()
  File "/usr/lib/pymodules/python2.7/matplotlib/dates.py", line 530, in viewlim_to_dt
    return num2date(vmin, self.tz), num2date(vmax, self.tz)
  File "/usr/lib/pymodules/python2.7/matplotlib/dates.py", line 289, in num2date
    if not cbook.iterable(x): return _from_ordinalf(x, tz)
  File "/usr/lib/pymodules/python2.7/matplotlib/dates.py", line 203, in _from_ordinalf
    dt = datetime.datetime.fromordinal(ix)
ValueError: year is out of range

我是python的新手,我尝试了一些似乎不起作用的东西。我该如何解决这个问题?


我按照建议尝试重新映射,似乎正在运行:

results=[[1388897101, 24.52, 24.51, 24.52, 24.51, 24.51562500000004, 58.848754], [1388896981, 24.51, 24.54544, 24.54544, 24.51, 24.521243333333334, 44.292754], [1388896861, 24.546877777777777, 24.5, 24.575633333333332, 24.5, 24.523650277777747, 71.937206], [1388896741, 24.5, 24.5, 24.5, 24.5, 24.5, 27.362592], [1388896621, 24.5, 24.516213513513513, 24.516213513513513, 24.5, 24.504934459459466, 87.731023]]

i=0;
for data in results:
    results[i][0] = epoch2num(data[0])
    i+=1

results=[[735238.19792824076, 24.52, 24.51, 24.52, 24.51, 24.51562500000004, 58.848754], [735238.19653935183, 24.51, 24.54544, 24.54544, 24.51, 24.521243333333334, 44.292754], [735238.19515046291, 24.546877777777777, 24.5, 24.575633333333332, 24.5, 24.523650277777747, 71.937206], [735238.1937615741, 24.5, 24.5, 24.5, 24.5, 24.5, 27.362592], [735238.19237268518, 24.5, 24.516213513513513, 24.516213513513513, 24.5, 24.504934459459466, 87.731023]]

但x轴上的比例涵盖数月,而不仅仅是几个小时的数据。你如何调整日期的比例?

1 个答案:

答案 0 :(得分:3)

matplotlib根据matplotlib.dates documentation使用不同的时间编号:

  

Matplotlib提供复杂的日期绘图功能   在python datetime的肩膀上,附加模块pytz和   dateutils。 datetime对象转换为浮点数   表示自0001-01-01 UTC以来的天数加1 。对于   例如,0001-01-01,06:00是1.25,而不是0.25。

您需要使用matplotlib.dates.epoch2num转换纪元时间戳:

results=[
    ...
]
for data in results:
    data[0] = epoch2num(data[0]) # <---