Search code examples
mplfinance

Mplfinance and adding fill_between on subplot


I want to use 'fill_between' attribute on a subplot but I am unsuccessful.

Attribute works when not using on a subplot as per below example and screenshot:

list_dates = [0,1,2,3,4,5]
list_values = [10,11,12,13,14,15]

df = pd.DataFrame(list(zip(list_dates,list_values, list_values, list_values, list_values)), columns=['Dates', 'Open', 'High', 'Low', 'Close'])
df.index = pd.date_range('1/1/2021',periods=(len(df)))

mpf.plot(df, type='line', ylim=(0,20), fill_between=df['Close'].values)

enter image description here

But when I try to get the same result using subplot the attribute does not seem to apply. What am I missing? Thanks in advance

list_dates = [0,1,2,3,4,5]
list_values = [10,11,12,13,14,15]

df = pd.DataFrame(list(zip(list_dates,list_values, list_values, list_values, list_values)), columns=['Dates', 'Open', 'High', 'Low', 'Close'])
df.index = pd.date_range('1/1/2021',periods=(len(df)))

fig = mpf.figure(figsize=(12,9))
ax1 = fig.add_subplot(2,2,1)
mpf.plot(df,type='line', ylim=(0,20), ax=ax1,fill_between=df['Close'].values)
mpf.show()

enter image description here


Solution

  • fill_between does work for "subplots," but only when those subplots are created using mplfinance.make_addplot().

    You, however, are creating your subplot using external axes mode. Mplfinance does not support fill_between in external axes mode because, in external axes mode, the user owns the Axes object, (not mplfinance) and the user can easily call fill_between() directly on the Axes object*.


    As noted in the external axes mode documentation, the external axes method of subplotting is strongly discouraged, because you lose the benefits of mplfinance doing much of the work for you. One should only use external axes mode when there is no other way to accomplish what you are trying to do. Full disclosure: I am the maintainer of mplfinance.