尝试使用candlestick_ohlc

时间:2018-03-13 07:55:27

标签: python matplotlib

我正在尝试根据API中收集的数据构建一个烛台,下面是一个简单的数据列表,其中包含日期打开高点低收盘价和成交量:

[[1502928000000, 4261.48, 4485.39, 4200.74, 4285.08, 795.150377], 
[1503014400000, 4285.08, 4371.52, 3938.77, 4108.37, 1199.888264], 
[1503100800000, 4108.37, 4184.69, 3850.0, 4139.98, 381.309763], 
[1503187200000, 4120.98, 4211.08, 4032.62, 4086.29, 467.083022], 
[1503273600000, 4069.13, 4119.62, 3911.79, 4016.0, 691.74306], 
[1503360000000, 4016.0, 4104.82, 3400.0, 4040.0, 966.684858], 
[1503446400000, 4040.0, 4265.8, 4013.89, 4114.01, 1001.136565], 
[1503532800000, 4147.0, 4371.68, 4085.01, 4316.01, 787.418753]]

我注意到在时间戳的末尾我有3个额外的0,所以我创建了这个代码来删除它们并将它们转换为时间:

for i in ohlc:
Date = str(i[0])
last=len(Date)-3
Date =Date[:last]
Date=float(Date)
Date = datetime.datetime.fromtimestamp(Date).strftime('%d-%m-%y %H:%M:%S')
i[0] =Date
print(i[0])

现在的日期是这种格式:13-03-18 01:00:00 现在问题当我尝试使用matplotlib绘制此代码时:

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

date_fmt = '%d-%m-%y %H:%M:%S'
fig.subplots_adjust(bottom=0.2)

ax1.xaxis.set_major_formatter(mdates.DateFormatter('%y-%m-%d'))

#ax1.xaxis.set_major_locator(mticker.MaxNLocator(10))
for label in ax1.xaxis.get_ticklabels():
    label.set_rotation(45)
plt.xlabel('Date')
plt.ylabel('Price')
plt.title(symbol)
#plt.legend()
plt.subplots_adjust(left=0.09, bottom=0.20, right=0.94, top=0.90, wspace=0.2, hspace=0)
plt.show()

我有一个错误,我认为问题出在日期。也许值得一提的是,我试图使用列表而不转换时间戳,但它没有工作错误:

Traceback (most recent call last):
File "chartingV2.py", line 84, in <module>
candlestick_ohlc(ax1,ohlc,width=0.002)
File "/usr/local/lib/python3.5/dist-packages/mpl_finance-0.10.0-
py3.5.egg/mpl_finance.py", line 236, in candlestick_ohlc
File "/usr/local/lib/python3.5/dist-packages/mpl_finance-0.10.0-
py3.5.egg/mpl_finance.py", line 302, in _candlestick
TypeError: unsupported operand type(s) for -: 'str' and 'float'

1 个答案:

答案 0 :(得分:1)

由于mpl_finance.candlestick_xxx绘图无法绘制字符串,因此您需要使用日期的数字表示。为了使matplotlib能够解释这些数字,您可以使用matplotlib.dates.dates2num()函数。

因此用

替换for循环
for row in ohlc:
    row[0] = mdates.date2num(datetime.datetime.fromtimestamp(row[0]/1000.))

完整代码:

import datetime
import matplotlib.pyplot as plt
from mpl_finance import candlestick_ohlc
import matplotlib.dates as mdates

ohlc = [[1502928000000, 4261.48, 4485.39, 4200.74, 4285.08, 795.150377], 
        [1503014400000, 4285.08, 4371.52, 3938.77, 4108.37, 1199.888264], 
        [1503100800000, 4108.37, 4184.69, 3850.0, 4139.98, 381.309763], 
        [1503187200000, 4120.98, 4211.08, 4032.62, 4086.29, 467.083022], 
        [1503273600000, 4069.13, 4119.62, 3911.79, 4016.0, 691.74306], 
        [1503360000000, 4016.0, 4104.82, 3400.0, 4040.0, 966.684858], 
        [1503446400000, 4040.0, 4265.8, 4013.89, 4114.01, 1001.136565], 
        [1503532800000, 4147.0, 4371.68, 4085.01, 4316.01, 787.418753]]

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

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

date_fmt = '%d-%m-%y %H:%M:%S'
fig.subplots_adjust(bottom=0.2)

ax1.xaxis.set_major_formatter(mdates.DateFormatter('%y-%m-%d'))

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

enter image description here