I am trying to make a typical finance OHLC graph with a subplot that has a volume bar chart. I am trying to use the volume_overlay2 function but it seems to be plotting the volume starting from 0 on the x-axis (see below picture):
My question is are there a way to force the data to be plotted underneath the OHLC accordingly or a way to shift the volume graph along the x-axis to the appropriate position?
I have tried using 'Date'
as the index for df_vol
and df_close
but it still wont work.
Please find below the code I used:
import matplotlib.pyplot as plt
from matplotlib import style
import quandl
from matplotlib.finance import volume_overlay2, candlestick_ohlc
import matplotlib.dates as mdates
style.use('ggplot')
rawdf = quandl.get("BITFINEX/BTCUSD")
df_ohlc = rawdf.drop(["Mid", "Bid", "Ask"], 1)
df_ohlc["Open"] = rawdf.Last.shift(1)
df_ohlc.rename(columns = {"Last":"Close"}, inplace = True)
df_ohlc.reset_index(inplace = True)
df_ohlc['Date'] = df_ohlc['Date'].map(mdates.date2num)
df_ohlc.dropna(inplace = True)
df_ohlc = df_ohlc[['Date','Open', 'High', 'Low', 'Close', 'Volume']]
df_close = df_ohlc['Close']
df_vol = df_ohlc['Volume']
fig = plt.figure()
ax1 = plt.subplot2grid((6,1), (0,0), rowspan = 5, colspan = 1)
ax2 = plt.subplot2grid((6,1), (5,0), rowspan = 1, colspan = 1, sharex = ax1)
#ax1.set_xlim(df_ohlc['Date'].iloc[0], df_ohlc['Date'].iloc[-1])
candlestick_ohlc(ax1, df_ohlc.values, colorup = 'g', colordown = 'r', width = 0.8)
vc = volume_overlay2(ax2, df_close, df_vol.values, colorup = 'g', colordown = 'r', width = 0.8, alpha = 1.0)
ax2.add_collection(vc, autolim = True)
plt.show()