matplotlib / mplfinance

Financial Markets Data Visualization using Matplotlib
https://pypi.org/project/mplfinance/
Other
3.59k stars 621 forks source link

Support `fill_between` in "External Axes" Mode. And Support multiple `fill_between`s #291

Open qililiu opened 3 years ago

qililiu commented 3 years ago

Describe the bug in "External Axes Method", the 'fill_between' function does not work.

To Reproduce `import mplfinance as mpf idf = pd.read_csv('data/SPY_20110701_20120630_Bollinger.csv',index_col=0,parse_dates=True)

fig = mpf.figure(style='yahoo',figsize=(7,8))

ax1 = fig.add_subplot(2,1,1)

ax2 = fig.add_subplot(2,1,2)

mpf.plot(idf,ax=ax1,volume=ax2,fill_between=130) `

Expected behavior An candlestick plot with shaded region at prices 0-130.

Screenshots Figure 2020-11-11 222511

Desktop (please complete the following information):

DanielGoldfarb commented 3 years ago

This is not a bug, but by design, when in "external axes mode," the fill_between kwarg gets ignored. There are several kwargs that get ignored when in "external axes mode", most of which display a warning if the user sets that kwarg and external axes mode at the same time. This one probably should also display a warning.

That said, I am having doubts about whether or not to keep this design. My original thinking was that since, in external axes mode, the user already has full control over the axes, then the user can call Axes.fill_between() themselves. (That can also serve as a possible work-around for the fact that presently only one fill_between is supported, see #218). However calling Axes.fill_between(), directly, presents certain problems for the caller, in regards to the x-axis, as described in this comment.

The more I think about it, the more I think that the caller should not have to worry about those x-axis issues. Rather mplfinance should handle that internally. Therefore, in addition to adding support for multiple fill_between's, I think we should also enhance the code to support fill_betweens even in external axis mode. The only issue with doing that is we will have to come up with a way to make it clear to which Axes object each fill_between specification should be applied.

I hope that makes sense. Please let me know if you have any other questions.

All the best. --Daniel

qililiu commented 3 years ago

Thank you Daniel for your comment. Looking forward to the future updates on these features.