首页 > 解决方案 > 使用烛台_ohlc 为时间戳使用 date2num 的正确方法是什么

问题描述

我的数据看起来像这样(Date, Open, High, Low, Close):

ohlc = [
        [1502929058, 1.2652, 1.2653, 1.265, 1.2653], 
        [1502929059, 1.267, 1.267, 1.267, 1.267], 
        [1502929060, 1.2655, 1.2656, 1.2655, 1.2656], 
        [1502929061, 1.2652, 1.2653, 1.2652, 1.2653], 
        [1502929062, 1.2631, 1.2631, 1.263, 1.2631], 
        [1502929063, 1.2625, 1.2625, 1.2625, 1.2625], 
        [1502929064, 1.2619, 1.2619, 1.2619, 1.2619], 
        [1502929065, 1.2622, 1.2623, 1.2622, 1.2623], 
        [1502929066, 1.2622, 1.2623, 1.2622, 1.2623], 
        [1502929067, 1.2617, 1.262, 1.2617, 1.262]
]

我正在使用代码来绘制烛台:

for row in ohlc:
    row[0] = mdates.date2num(datetime.datetime.fromtimestamp(row[0]))

fig = plt.figure()
ax1 = plt.subplot2grid((1,1), (0,0))
candlestick_ohlc(ax1,ohlc,width=0.1)

fig.subplots_adjust(bottom=0.3)

ax1.xaxis.set_major_formatter(mdates.DateFormatter('%y-%m-%d %H:%M:%S'))

for label in ax1.xaxis.get_ticklabels():
    label.set_rotation(45)
plt.xlabel('Date')
plt.ylabel('Price')
plt.show()

但烛台被绘制在彼此之上:

在此处输入图像描述

当我进一步检查代码时,我注意到它mdates.date2num(datetime.datetime.fromtimestamp(row[0]))实际上生成的日期差异非常小(因此烛台被绘制在彼此之上):

736558.1997453704
736558.1997569444
736558.1997685186
736558.1997800926
736558.1997916667
736558.1998032407
736558.1998148148
736558.1998263889
736558.199837963
736558.199849537

这个问题的解决方案是什么?

标签: python-3.xmatplotlibcandlestick-chart

解决方案


推荐阅读