Python Pyplot正确的烛台时间戳纪元变化和规模

时间:2014-01-05 05:34:06

标签: python matplotlib

我想知道如何正确处理unix时间戳并将它们绘制为具有适当x轴刻度的日期字符串(相对于x轴时间帧,例如分钟,小时,天......)。

我有一个带有数字时间戳的烛台图(使用unix纪元)。这样的样本间隔120秒:

candlestick

使用此代码:

import datetime
import matplotlib.pyplot as plt
from matplotlib.dates import  DateFormatter, epoch2num
from matplotlib.finance import candlestick

results=[[1388898634, 24.339, 24.24389, 24.376, 24.22290666666667, 24.247343013888855, 1724.3410760000002], [1388898514, 24.2444, 24.26181818181818, 24.28, 24.2444, 24.25408960227278, 1875.784091], [1388898394, 24.262727272727272, 24.310416666666665, 24.310416666666665, 24.262727272727272, 24.295351803977244, 389.329334], [1388898274, 24.310499999999998, 24.3996, 24.3996, 24.310499999999998, 24.33321454274891, 536.4064960000003], [1388898154, 24.3998, 24.45051724137931, 24.45051724137931, 24.3998, 24.423427643678153, 313.12763599999994]]

fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)
candlestick(ax, results, width=20)
ax.autoscale_view()
plt.setp( plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')
plt.show()

现在我想用日期格式绘制这个,所以我尝试将它移到正确的时代:

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

对于紧凑性,这导致了这个:

results=[[735238.21567129635, 24.339, 24.24389, 24.376, 24.22290666666667, 24.247343013888855, 1724.3410760000002], [735238.21428240743, 24.2444, 24.26181818181818, 24.28, 24.2444, 24.25408960227278, 1875.784091], [735238.2128935185, 24.262727272727272, 24.310416666666665, 24.310416666666665, 24.262727272727272, 24.295351803977244, 389.329334], [735238.21150462958, 24.310499999999998, 24.3996, 24.3996, 24.310499999999998, 24.33321454274891, 536.4064960000003], [735238.21011574077, 24.3998, 24.45051724137931, 24.45051724137931, 24.3998, 24.423427643678153, 313.12763599999994]]

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

candlestick(ax, results, width=20)
ax.autoscale_view()
plt.setp( plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')
plt.show()

但烛台情节遭到破坏: candlestick2

我也尝试使用格式化程序映射x轴:

results=[[735238.21567129635, 24.339, 24.24389, 24.376, 24.22290666666667, 24.247343013888855, 1724.3410760000002], [735238.21428240743, 24.2444, 24.26181818181818, 24.28, 24.2444, 24.25408960227278, 1875.784091], [735238.2128935185, 24.262727272727272, 24.310416666666665, 24.310416666666665, 24.262727272727272, 24.295351803977244, 389.329334], [735238.21150462958, 24.310499999999998, 24.3996, 24.3996, 24.310499999999998, 24.33321454274891, 536.4064960000003], [735238.21011574077, 24.3998, 24.45051724137931, 24.45051724137931, 24.3998, 24.423427643678153, 313.12763599999994]]

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()

但是在损坏的烛台之上,它还会缩短日期时间轴的时间跨度。

datetime axis

为什么在重新映射时间戳时,我的烛台图会被破坏?如何在使用日期时正确缩放x轴(在这种情况下,我需要显示分钟增量,但我可能希望跨越几天或几个月,具体取决于采样率)?

谢谢!

1 个答案:

答案 0 :(得分:2)

width=1函数的{p> candlestick代表1天,而不是1个像素。

根据candlestick

的文档字符串
  

烛台(ax,quotes,width = 0.2,colorup ='k',colordown ='r',   阿尔法= 1.0)

     

...

     

宽度:一天的一小部分,矩形宽度

将其替换为30 / 86400.0之类的小值(代表30秒):

candlestick(ax, results, width=30/86400.0)

enter image description here