【问题标题】:Python Pyplot unix timestamp on axis with candlestick plot带有烛台图的轴上的 Python Pyplot unix 时间戳
【发布时间】:2014-01-05 03:53:30
【问题描述】:

我尝试过一些关于使用 pyplot 将时间戳绘制为字符串的答案,但没有一个答案能完全解决我的情况。

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

我知道我需要将它们转换为 datetime 对象才能使格式化程序工作,但我不确定用这种类型的绘图完成此操作的最佳方法是什么。这就是我现在拥有的:

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 轴上的比例涵盖了几个月,而不仅仅是几个小时的数据。如何调整日期的比例?

【问题讨论】:

    标签: python datetime matplotlib


    【解决方案1】:

    matplotlib 使用不同的时间数字,根据matplotlib.dates documentation:

    Matplotlib 提供复杂的日期绘图功能,常设 在 python datetime 的肩膀上,附加模块 pytz 和 日期工具。 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]) # <---
    

    【讨论】:

    • 谢谢!这是一个很好的洞察力。日期现在看起来在正确的范围内,但是如何正确缩放轴?见编辑。
    • @user6972,对不起,我不知道。发布一个单独的问题怎么样?
    猜你喜欢
    • 2023-04-02
    • 1970-01-01
    • 2015-11-20
    • 2019-04-05
    • 1970-01-01
    • 2014-04-10
    • 2019-02-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多