几分钟后的 Matplotlib 烛台 [英] Matplotlib candlestick in minutes

查看:62
本文介绍了几分钟后的 Matplotlib 烛台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下午好,

我想看看你们中是否有人能帮我在几分钟内制作一个蜡烛图.我已经在几天内成功绘制了图表,但是我不知道如何在几分钟内完成.

I would like to see if any of you could help me make a candle chart in minutes. I have managed to graph them in days but I do not know how to do them in minutes.

附加代码.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import dates, ticker
import matplotlib as mpl
from mpl_finance import candlestick_ohlc

mpl.style.use('default')

data = [('2017-01-02 02:00:00', '1.05155', '1.05197', '1.05155', '1.0519'),
    ('2017-01-02 02:01:00', '1.05209', '1.05209', '1.05177', '1.05179'),
    ('2017-01-02 02:02:00', '1.05177', '1.05198', '1.05177', '1.05178'),
    ('2017-01-02 02:03:00', '1.05188', '1.052', '1.05188', '1.052'),
    ('2017-01-02 02:04:00', '1.05196', '1.05204', '1.05196', '1.05203'),
    ('2017-01-02 02:06:00', '1.05196', '1.05204', '1.05196', '1.05204'),
    ('2017-01-02 02:07:00', '1.05205', '1.0521', '1.05205', '1.05209'),
    ('2017-01-02 02:08:00', '1.0521', '1.0521', '1.05209', '1.05209'),
    ('2017-01-02 02:09:00', '1.05208', '1.05209', '1.05208', '1.05209'),
    ('2017-01-02 02:10:00', '1.05208', '1.05211', '1.05207', '1.05209')]

ohlc_data = []

for line in data:
    ohlc_data.append((dates.datestr2num(line[0]), np.float64(line[1]), np.float64(line[2]), np.float64(line[3]), np.float64(line[4])))

fig, ax1 = plt.subplots()
candlestick_ohlc(ax1, ohlc_data, width = 0.5, colorup = 'g', colordown = 'r', alpha = 0.8)

ax1.xaxis.set_major_formatter(dates.DateFormatter('%d/%m/%Y %H:%M'))
ax1.xaxis.set_major_locator(ticker.MaxNLocator(10))

plt.xticks(rotation = 30)
plt.grid()
plt.xlabel('Date')
plt.ylabel('Price')
plt.title('Historical Data EURUSD')
plt.tight_layout()
plt.show()

推荐答案

如此接近,但只有反复试验才能让您更进一步.糟糕的文档不是很好吗?

So close, but only trial and error will get you any further. Isn't crappy documentation great?

只需将 width 除以一天中的分钟数即可.您的副本和完整代码在下面粘贴乐趣,但我所做的只是将 width = 0.5 更改为 width = 0.5/(24*60).

Simply divide width by the number of minutes in a day. Full code for your copy & paste pleasure below, but all I've done is change width = 0.5 to width = 0.5/(24*60).

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import dates, ticker
import matplotlib as mpl
from mpl_finance import candlestick_ohlc

mpl.style.use('default')

data = [('2017-01-02 02:00:00', '1.05155', '1.05197', '1.05155', '1.0519'),
    ('2017-01-02 02:01:00', '1.05209', '1.05209', '1.05177', '1.05179'),
    ('2017-01-02 02:02:00', '1.05177', '1.05198', '1.05177', '1.05178'),
    ('2017-01-02 02:03:00', '1.05188', '1.052', '1.05188', '1.052'),
    ('2017-01-02 02:04:00', '1.05196', '1.05204', '1.05196', '1.05203'),
    ('2017-01-02 02:06:00', '1.05196', '1.05204', '1.05196', '1.05204'),
    ('2017-01-02 02:07:00', '1.05205', '1.0521', '1.05205', '1.05209'),
    ('2017-01-02 02:08:00', '1.0521', '1.0521', '1.05209', '1.05209'),
    ('2017-01-02 02:09:00', '1.05208', '1.05209', '1.05208', '1.05209'),
    ('2017-01-02 02:10:00', '1.05208', '1.05211', '1.05207', '1.05209')]

ohlc_data = []

for line in data:
    ohlc_data.append((dates.datestr2num(line[0]), np.float64(line[1]), np.float64(line[2]), np.float64(line[3]), np.float64(line[4])))

fig, ax1 = plt.subplots()
candlestick_ohlc(ax1, ohlc_data, width = 0.5/(24*60), colorup = 'g', colordown = 'r', alpha = 0.8)

ax1.xaxis.set_major_formatter(dates.DateFormatter('%d/%m/%Y %H:%M'))
ax1.xaxis.set_major_locator(ticker.MaxNLocator(10))

plt.xticks(rotation = 30)
plt.grid()
plt.xlabel('Date')
plt.ylabel('Price')
plt.title('Historical Data EURUSD')
plt.tight_layout()
plt.show()

这篇关于几分钟后的 Matplotlib 烛台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆