我正在尝试使用Matplotlib.finance库绘制一些财务数据,并且candlestick2部分工作正常。但是,尽管第二轴已正确缩放,但是`volume_overlay函数在绘图上未显示任何内容。

还有一个类似的问题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函数损坏了?

编辑

数据从http://api.bitcoincharts.com/v1/trades.csv?symbol=mtgoxUSD下载-粘贴到Notepad ++中,然后搜索并用“ \ n”替换“”。

最佳答案

有一个非常愚蠢的错误(或者可能是奇怪的设计选择),因为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中发布]

关于python - 在matplotlib中添加体积叠加,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17251871/

10-11 22:04
查看更多