我正在尝试使用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”替换“”。
答案 0 :(得分:2)
有一个非常愚蠢的错误(或者可能是奇怪的设计选择),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中]