Adding volume overlay in matplotlib

I am trying to build some financial data using the Matplotlib.finance , and part of candlestick2 working fine. However, the `volume_overlay 'function does not show anything on the graph, although the second axis scales correctly.

There is a similar question here, but it does not solve the problem, it simply provides a way to create 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 someone show me what I am missing? Or is the volume_overlay function volume_overlay ?

EDIT

Data is downloaded from http://api.bitcoincharts.com/v1/trades.csv?symbol=mtgoxUSD - inserted into Notepad ++, and then the search for and replacing "" with "\ n" is performed.

0
source share
1 answer

There is a very silly mistake (or maybe a strange design choice) in which 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 version 1.3.0 when submitted]

+1
source

All Articles