gpt4 book ai didi

Python Pyplot 适当的烛台时间戳纪元变化和比例

转载 作者:行者123 更新时间:2023-11-28 17:43:43 26 4
gpt4 key购买 nike

我想知道如何正确处理 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 轴(在这种情况下我需要显示分钟增量,但我可能希望根据采样率跨越几天或几个月)?

谢谢!

最佳答案

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

根据 candlestick 的文档字符串:

candlestick(ax, quotes, width=0.2, colorup='k', colordown='r', alpha=1.0)

...

width : fraction of a day for the rectangle width

将其替换为较小的值,例如 30/86400.0(代表 30 秒):

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

enter image description here

关于Python Pyplot 适当的烛台时间戳纪元变化和比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20930488/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com