1
votes

I'm trying to plot some financial data using Matplotlib.finance library and the candlestick2 part is working fine. However the `volume_overlay function is not showing anything on the plot, although the second axis is being scaled correctly.

There's a similar question here but it doesn't resolve the issue, just provides a way of creating your own 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()

Can anyone show me what I'm missing? Or is the volume_overlay function broken?

EDIT

The data is downloaded from http://api.bitcoincharts.com/v1/trades.csv?symbol=mtgoxUSD - pasted into Notepad++ and then search and replaced " " with "\n".

1
As a warning, that module is going to get a major api-change in the near future github.com/matplotlib/matplotlib/pull/1920 If you actually use this module, please comment ;) - tacaswell
and can you post the csv you are using? - tacaswell
What is candlestick2? - tommy.carstensen
@tommy.carstensen I think it's now renamed to candlestick2_ochl - but here are the docs from when I asked the question het.as.utexas.edu/HET/Software/Matplotlib/api/finance_api.html - Jamie Bull

1 Answers

2
votes

There is a very silly bug (or maybe strange design choice) in that volume_overlay returns a polyCollection, but does not add it to the axes. The following should work:

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 [This has been fixed and will be in 1.3.0 when it ships]