2014-01-05 32 views
2

我試圖通過一些使用pyplot繪製時間戳作爲字符串的答案,但沒有一個答案完全解決了我的情況。python pyplot軸上的unix時間戳與燭臺圖

這是我試圖做的數據組織爲[時間戳,打開,關閉,最大,最小,平均,體積]的示例。蠟燭圖工作正常,但我試圖添加顯示日期時間的X軸。

我知道我需要將它們轉換爲格式化程序工作的日期時間對象,但我不確定用這種類型的繪圖來完成此操作的最佳方式是什麼。這是我現在有:

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

將會產生一個錯誤,我相信有關,不是一個DateTime對象:

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 

我是新來的蟒蛇,我已經試過幾件事情這似乎並不奏效。我該如何解決這個問題?


我試圖重新映射的建議,它似乎是工作:

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軸上的規模佔地個月,而不是僅僅只有幾個小時的數據。你如何調整日期的比例?

回答

3

matplotlib使用不同數量的時間,根據matplotlib.dates documentation

Matplotlib提供複雜的日期繪圖功能,Python的日期時間的肩膀上站着 的附加模塊pytz和 dateutils。日期時間對象轉換爲浮點數 ,它表示自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]) # <--- 
+0

謝謝!這是一個很好的見解。日期看起來像他們現在在正確的範圍內,但我如何正確地縮放軸?請參閱編輯。 – user6972

+0

@ user6972,對不起,我不知道。如何發佈一個單獨的問題? – falsetru