Python Pyplot适当的烛台时间戳时代变化和比例
我想知道如何正确处理unix时间戳,并将其绘制为具有适当x轴刻度(相对于x轴时间框架,如分钟、小时、天…)的日期字符串 我有一个带有数字时间戳的烛台图(使用unix历元)。采样间隔120秒时,它看起来是这样的: 使用此代码:Python Pyplot适当的烛台时间戳时代变化和比例,python,matplotlib,Python,Matplotlib,我想知道如何正确处理unix时间戳,并将其绘制为具有适当x轴刻度(相对于x轴时间框架,如分钟、小时、天…)的日期字符串 我有一个带有数字时间戳的烛台图(使用unix历元)。采样间隔120秒时,它看起来是这样的: 使用此代码: import datetime import matplotlib.pyplot as plt from matplotlib.dates import DateFormatter, epoch2num from matplotlib.finance import ca
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()
但烛台的阴谋被摧毁了:
我还尝试使用如下格式器映射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()
但在破损的烛台上,它也会将日期时间轴缩放太长
当我重新映射时间戳时,为什么我的烛台图会被破坏?在使用日期时,如何正确缩放x轴(在本例中,我需要显示分钟增量,但我可能希望根据采样率跨越几天或几个月)
谢谢
width=1
的烛台
功能表示1天,而不是1个像素
根据烛台的文档字符串:
烛台(ax,引号,宽度=0.2,上色为'k',下色为'r',
α=1.0)
宽度:一天的分数用于矩形宽度
将其替换为小值,如30/86400.0
(表示30秒):
谢谢。我很难找到参数的定义。我忘了python有docstring和help()函数。@user6972,我在matplotlib文档中找不到
candlestick
函数的文档
candlestick(ax, results, width=30/86400.0)