Search code examples
pythonpython-3.xmatplotlibmplfinance

How do i draw on top of a chart in Matplotlib Finance?


I created a chart using MPL Finance, here is how i did it:

chart = mpf.plot(df, type='candle', figratio=(20,9), style="nightclouds", savefig='plottedCharts/TEST.png', volume=True)

Where df is my Pandas dataframe. I'm trying to add square dots to some important points on the chart, like this:

plt.scatter('2020-05-16 13:30', 9300, marker='s', c='b', s=1)

But when i try this, nothing appears on the chart. Is there any way to achieve this?


Solution

  • Yes. You call mpf.make_addplot() (make additional plot data)

    And pass the result from make_addplot into mpf.plot() using the addplot kwarg:

    ap = mpf.make_addplot(signal,type=scatter,marker='s',color='b')
    mpf.plot(df, type='candle', figratio=(20,9), style="nightclouds",
             savefig='plottedCharts/TEST.png', volume=True, addplot=ap)
    

    The variable signal is a list, series, or dataframe that is the same length as df.

    Set signal to the appropriate price value where you want the squares to appear. All other values of signal should be filled with float('nan') (to ensure that it is the same length as df).

    You can see specific examples doing exactly what you want in the tutorial here: https://github.com/matplotlib/mplfinance/blob/master/examples/addplot.ipynb