mplfinance icon indicating copy to clipboard operation
mplfinance copied to clipboard

Feature Request: Ability to overlay a candle chart over another (share axes)

Open GAEfan opened this issue 5 years ago • 5 comments

Thank you for your work on updating mplfinance!

Using the deprecated mpl-finance, I was able to put 2 candlestick_ohlc() charts on top of each other (overlaid, sharing the same axes), and a table below the chart, all within a single figure that could be saved.

Now, I can use returnfig=True to export the figure and axes, but not sure how to actually use those later. ax is not a valid kwarg in plot(), as it was in candlestick_ohlc(). And not sure how to draw the chart inside a parent figure. Should fig be a kwarg for plot()?

Thank you.

GAEfan avatar Apr 03 '20 22:04 GAEfan

Ability to create and pass in your own Figure and Axes is coming. Maybe another 4 to 6 weeks. In the meantime, you may be able to use the Figure and Axes that are returned from returnfig=True kwarg.

I'd like to see an example of an two overlayed candle charts, if you could point me to an example that would be great. Thanks.

DanielGoldfarb avatar Apr 03 '20 22:04 DanielGoldfarb

Hi Daniel,

Thank you for the prompt response. Yes, I can gather Figure and Axes from returnfig=True. However, how do I tell the second mfp.plot() to use the same axes? ax= is not yet part of the kwargs.

Here is an example of candles sharing the same axes via overlay. It is from a machine learning prediction machine: image

GAEfan avatar Apr 03 '20 23:04 GAEfan

This can be handled with a couple of lines of code:

In _valid_plot_kwargs():

'fig'         : { 'Default'     : None,
                  'Validator'   : lambda value: isinstance(value,object) },

In plot():

fig = config['fig'] or plt.figure()

GAEfan avatar Apr 14 '20 15:04 GAEfan

@GAEfan --

Do you have a working version of the code, with this change, that does the overlay that you want with mplfinance? I would like to look at it.

DanielGoldfarb avatar Apr 14 '20 16:04 DanielGoldfarb

@GAEfan

Thank you for the prompt response. Yes, I can gather Figure and Axes from returnfig=True. However, how do I tell the second mfp.plot() to use the same axes? ax= is not yet part of the kwargs.

There have been a number of enhancements since this issue was first posted.

There now is kwarg ax= which you can use. So you can do:

fig, axlist = mpf.plot(df1,...,returnfig=True)

mpf.plot(df2,ax=axlist[0])

Please let me know if this is an adequate solution for your issue, or if we can do something else for you.
For more information see https://github.com/matplotlib/mplfinance/wiki/Acessing-mplfinance-Figure-and-Axes-objects

Thank you. All the best. --Daniel

DanielGoldfarb avatar Feb 19 '21 19:02 DanielGoldfarb