0

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()
Vinícius Figueiredo
  • 6,300
  • 3
  • 25
  • 44

1 Answers1

0

Have a look : Matplotlib - Finance volume overlay

This question might be useful for you. Nowadays finance module from matplotlib is deprecated and on its place there is a solution called mpl_finance. But it is not under maintenance so the issues from the last package are still without solution. I´ve tried to incorporate overlay command but it has the same problems...So I´ve chosen to implement the solution described on that post. I hope give u a solution for your problem.

I use pandas and everything is OK. Except...I don´t know how to fix the size of the axis (twinx()) that we create for the volume. I´ve tried Bbox but without succeed... Regards

Jorge
  • 111
  • 2