1

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".

Community
  • 1
  • 1
Jamie Bull
  • 12,889
  • 15
  • 77
  • 116

1 Answers1

2

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]

tacaswell
  • 84,579
  • 22
  • 210
  • 199
  • I pass `times` to the function `candlestick_ohlc`, which is equal to `times = mdates.date2num(bars.index.to_pydatetime())`. How do I make the volume overlay appear on the same axis with the same x-ticks? – tommy.carstensen Sep 01 '18 at 04:08