2012-10-29 261 views
7

我正在製作兩個數據集:[開,高,低,關閉]和音量的燭臺圖表。我想在圖表中這樣的底部覆蓋卷:Matplotlib - 金融音量覆蓋

finviz.com

我打電話volume_overlay3但不是吧填滿整個繪圖區。我究竟做錯了什麼?

volume_overlay3

我的另一種選擇是使用的.bar(),它不具有向上和向下的顏色,但會工作,如果我能得到正確的規模:

enter image description here

fig = plt.figure() 
ax = fig.add_subplot(1,1,1) 

candlestick(ax, candlesticks) 

ax2 = ax.twinx() 

volume_overlay3(ax2, quotes) 

ax2.xaxis_date() 

ax2.set_xlim(candlesticks[0][0], candlesticks[-1][0]) 

ax.yaxis.set_label_position("right") 
ax.yaxis.tick_right() 

ax2.yaxis.set_label_position("left") 
ax2.yaxis.tick_left() 
+0

根本不是經濟學 - 那麼你的上/下功能是如何指定的(我認爲你可以給每個欄選擇兩個值之間的顏色取決於上/下規則...例如通過將信號與[-1 1]內核或類似的東西進行卷積運算,如果它不僅僅是缺少的'plt.hold(true)', – deinonychusaur

回答

22

volume_overlay3對我不起作用。所以我想你的想法添加一個條形圖陰謀陰謀。

創建卷的雙軸後,重新定位此軸(使其縮短)並修改燭臺y數據的範圍以避免碰撞。

import numpy as np 
import matplotlib 
import matplotlib.pyplot as plt 
# from matplotlib.finance import candlestick 
# from matplotlib.finance import volume_overlay3 
# finance module is no longer part of matplotlib 
# see: https://github.com/matplotlib/mpl_finance 
from mpl_finance import candlestick_ochl as candlestick 
from mpl_finance import volume_overlay3 
from matplotlib.dates import num2date 
from matplotlib.dates import date2num 
import matplotlib.mlab as mlab 
import datetime 

datafile = 'data.csv' 
r = mlab.csv2rec(datafile, delimiter=';') 

# the dates in my example file-set are very sparse (and annoying) change the dates to be sequential 
for i in range(len(r)-1): 
    r['date'][i+1] = r['date'][i] + datetime.timedelta(days=1) 

candlesticks = zip(date2num(r['date']),r['open'],r['close'],r['max'],r['min'],r['volume']) 

fig = plt.figure() 
ax = fig.add_subplot(1,1,1) 

ax.set_ylabel('Quote ($)', size=20) 
candlestick(ax, candlesticks,width=1,colorup='g', colordown='r') 

# shift y-limits of the candlestick plot so that there is space at the bottom for the volume bar chart 
pad = 0.25 
yl = ax.get_ylim() 
ax.set_ylim(yl[0]-(yl[1]-yl[0])*pad,yl[1]) 

# create the second axis for the volume bar-plot 
ax2 = ax.twinx() 


# set the position of ax2 so that it is short (y2=0.32) but otherwise the same size as ax 
ax2.set_position(matplotlib.transforms.Bbox([[0.125,0.1],[0.9,0.32]])) 

# get data from candlesticks for a bar plot 
dates = [x[0] for x in candlesticks] 
dates = np.asarray(dates) 
volume = [x[5] for x in candlesticks] 
volume = np.asarray(volume) 

# make bar plots and color differently depending on up/down for the day 
pos = r['open']-r['close']<0 
neg = r['open']-r['close']>0 
ax2.bar(dates[pos],volume[pos],color='green',width=1,align='center') 
ax2.bar(dates[neg],volume[neg],color='red',width=1,align='center') 

#scale the x-axis tight 
ax2.set_xlim(min(dates),max(dates)) 
# the y-ticks for the bar were too dense, keep only every third one 
yticks = ax2.get_yticks() 
ax2.set_yticks(yticks[::3]) 

ax2.yaxis.set_label_position("right") 
ax2.set_ylabel('Volume', size=20) 

# format the x-ticks with a human-readable date. 
xt = ax.get_xticks() 
new_xticks = [datetime.date.isoformat(num2date(d)) for d in xt] 
ax.set_xticklabels(new_xticks,rotation=45, horizontalalignment='right') 

plt.ion() 
plt.show() 

plot

data.csv是在這裏: http://pastebin.com/5dwzUM6e

+0

謝謝!這真是太棒了。如何調整音量圖的範圍這​​是有效的 – nathancahill

+0

超級清晰,有用的意見,關於數字/ fucntions正在做什麼,以及爲什麼 - 這正是我需要的。謝謝! – dwanderson

0

如果你想堆起來圖表彼此的頂部(即繪製它們在同一軸線上)使用方法:

plt.hold(True) 
1

看便知here。顯然是一個錯誤,它將被修復。

現在,您需要將volume_overlay3調用中返回的集合分配給一個變量,然後將其添加到圖表中。

vc = volume_overlay3(ax2, quotes) 
ax2.add_collection(vc) 
+0

我試過這個,但沒有得到音量疊加顯示在選定的答案中。您是否獲得了與所選答案中相同的音量疊加? – phan