当日蜡烛图使用Matplotlib

我在Matplotlib的财务图表上遇到了一些困难。 看起来他们的烛台图表在日常数据上效果最好,而且我很难让他们在盘中(每隔5分钟,9点半到4点之间)处理数据。

我在pastebin中粘贴了样本数据。 顶部是我从数据库中得到的东西,底部是格式化为一个序数浮动的date,用于Matplotlib。

链接到示例数据

当我绘制我的图表时,它们之间存在巨大的差距,轴线吸吮,缩放同样可怕。 http://imgur.com/y7O8A

在这里输入图像说明

如何从这些数据中创build一个可读的图表? 我的最终目标是获得一个像这样的远程图表:

在这里输入图像说明

View post on imgur.com

数据点可以从5分钟到30分钟不等。


我也做了pandas数据框的数据,但我不确定pandas是否有烛台function。

如果我理解的很好,你主要关心的是日常数据之间的差距。 为了摆脱它们,一种方法是人为地“平分”您的数据(但是当然,您将在一天之内失去任何暂时的指示)。

无论如何,这样做,你将能够获得一个图表,看起来像你提出的例子。

评论的代码和结果图如下。

import numpy as np import matplotlib.pyplot as plt import datetime from matplotlib.finance import candlestick from matplotlib.dates import num2date # data in a text file, 5 columns: time, opening, close, high, low # note that I'm using the time you formated into an ordinal float data = np.loadtxt('finance-data.txt', delimiter=',') # determine number of days and create a list of those days ndays = np.unique(np.trunc(data[:,0]), return_index=True) xdays = [] for n in np.arange(len(ndays[0])): xdays.append(datetime.date.isoformat(num2date(data[ndays[1],0][n]))) # creation of new data by replacing the time array with equally spaced values. # this will allow to remove the gap between the days, when plotting the data data2 = np.hstack([np.arange(data[:,0].size)[:, np.newaxis], data[:,1:]]) # plot the data fig = plt.figure(figsize=(10, 5)) ax = fig.add_axes([0.1, 0.2, 0.85, 0.7]) # customization of the axis ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') ax.tick_params(axis='both', direction='out', width=2, length=8, labelsize=12, pad=8) ax.spines['left'].set_linewidth(2) ax.spines['bottom'].set_linewidth(2) # set the ticks of the x axis only when starting a new day ax.set_xticks(data2[ndays[1],0]) ax.set_xticklabels(xdays, rotation=45, horizontalalignment='right') ax.set_ylabel('Quote ($)', size=20) ax.set_ylim([177, 196]) candlestick(ax, data2, width=0.5, colorup='g', colordown='r') plt.show() 

图形