1

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

这里有一个类似的问题,但它不能解决问题,只是提供了一种创建自己的卷覆盖的方法。

# 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”。

4

1 回答 1

2

有一个非常愚蠢的错误(或者可能是奇怪的设计选择)volume_overlay返回 a 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 中]

于 2013-06-22T15:51:26.787 回答