2013-06-22 51 views
0

我想要使用Matplotlib.finance庫和candlestick2部分工作正常,繪製一些財務數據。然而`volume_overlay函數沒有在圖上顯示任何東西,儘管第二個軸正在被正確縮放。在matplotlib中添加音量覆蓋

還有類似的問題here但它不能解決問題,只是提供了一種創建自己的音量疊加的方法。

# Get data from CSV 
data = pandas.read_csv('dummy_data.csv', 
          header=None, 
          names=['Time', 'Price', 'Volume']).set_index('Time') 

# Resample data into 30 min bins 
ticks = data.ix[:, ['Price', 'Volume']] 
bars = ticks.Price.resample('30min', how='ohlc') 
volumes = ticks.Volume.resample('30min', how='sum') 

# Create figure 
fig = plt.figure() 
ax1 = fig.add_subplot(111) 
# Plot the candlestick 
candles = candlestick2(ax1, bars['open'], bars['close'], 
         bars['high'], bars['low'], 
         width=1, colorup='g') 

# Add a seconds axis for the volume overlay 
ax2 = ax1.twinx() 

# Plot the volume overlay 
volume_overlay(ax2, bars['open'], bars['close'], volumes, colorup='g', alpha=0.5) 

plt.show() 

任何人都可以告訴我我失蹤了什麼嗎?或者是volume_overlay功能壞了?

EDIT

的數據從http://api.bitcoincharts.com/v1/trades.csv?symbol=mtgoxUSD下載 - 粘貼到記事本++,然後搜索並替換爲 「」 和 「\ n」 個。

+0

作爲一個警告,該模塊會在不久得到一個主要的API改變未來https://github.com/matplotlib/matplotlib/pull/1920如果你真的使用這個模塊,請評論;) – tacaswell

+0

,你可以發佈你正在使用的CSV? – tacaswell

回答

1

有一個非常愚蠢的錯誤(或可能是奇怪的設計選擇)volume_overlay返回polyCollection,但不會將它添加到軸。下面應該工作:

from matplotlib.finance import * 

data = parse_yahoo_historical(fetch_historical_yahoo('CKSW', (2013,1,1), (2013, 6, 1))) 

ds, opens, closes, highs, lows, volumes = zip(*data) 

# Create figure 
fig = plt.figure() 
ax1 = fig.add_subplot(111) 
# Plot the candlestick 
candles = candlestick2(ax1, opens, closes, highs, lows, 
         width=1, colorup='g') 

# Add a seconds axis for the volume overlay 
ax2 = ax1.twinx() 

# Plot the volume overlay 
bc = volume_overlay(ax2, opens, closes, volumes, colorup='g', alpha=0.5, width=1) 
ax2.add_collection(bc) 
plt.show() 

https://github.com/matplotlib/matplotlib/pull/2149 [該問題已解決,將在1.3.0當船舶]