Mplfinance: how to add legend to the plot?

Created on 18 Jun 2020  路  4Comments  路  Source: matplotlib/mplfinance

How to add a legend to the plots? For example, if using several moving averages it will be useful to show a legend to map moving averages to line plots.

Is clear how this is done using matplotlib but I did not see an example of how to do so using the mplfinance package.

TIA,

question

Most helpful comment

Plot chart to figure and axis object

    fig, axes = mpf.plot(df, type='candle', mav=mav_tuple, returnfig=True)
    # Configure chart legend and title
    axes[0].legend(mav_titles)
    axes[0].set_title(ticker)
    # Save figure to file
    fig.savefig(path_to_figure)

This does the trick thanks to the help of mr. Daniel Goldfarb.

All 4 comments

Rajeev,
Legends are not yet available in mplfinance. Hopefully soon (maybe a couple months or so).

In the meantime, if you want to play with matplotlib, you can try using the returnfig=True kwarg in mplfinance and attempt to display legends by having access to the Figure and Axes that mplfinance creates.

Alternatively if you would like to contribute code to mplfinance to make it easy for users to include legends, I would be happy to work together with you on getting that code correctly merged into mplfinance.

All the best. Thanks for you interest in mplfinance. --Daniel

Hi Daniel,
I appreciate your comments. I'll give it a try. This package seems to be going in a good direction. Can we have a side bar on how I can contribute code to the package? You can email me directly at [email protected].

All the best
--Rajeev

Plot chart to figure and axis object

    fig, axes = mpf.plot(df, type='candle', mav=mav_tuple, returnfig=True)
    # Configure chart legend and title
    axes[0].legend(mav_titles)
    axes[0].set_title(ticker)
    # Save figure to file
    fig.savefig(path_to_figure)

This does the trick thanks to the help of mr. Daniel Goldfarb.

@arthurpdesimone
Thank you for posting a specific example to help others. Much appreciated. All the best. --Daniel

Was this page helpful?
5 / 5 - 1 ratings

Related issues

kitt-th picture kitt-th  路  5Comments

Full4me picture Full4me  路  3Comments

obahat picture obahat  路  3Comments

tessanix picture tessanix  路  5Comments

tmcguire65 picture tmcguire65  路  6Comments