Skip to content

Un identified legends #497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Santunav opened this issue Jan 31, 2022 · 3 comments
Closed

Un identified legends #497

Santunav opened this issue Jan 31, 2022 · 3 comments
Labels
question Further information is requested

Comments

@Santunav
Copy link

Hi,

Can someone help me in identifying, new legends displayed on the plot. I'm adding 2 empty entries in mav_titles to match SMA colors

mav_titles = ['','','9 SMA' ,'50 SMA', '200 SMA' ]
fig, axes = mpf.plot(df,type='candle',mav=(9,50,200),volume=True,returnfig=True, style='yahoo')
axes[0].legend(mav_titles)
axes[0].set_title(ticker)
TCS

Thank you.

@Santunav Santunav added the question Further information is requested label Jan 31, 2022
@DanielGoldfarb
Copy link
Collaborator

DanielGoldfarb commented Jan 31, 2022

It appears to me that, in addition to the moving averages, legend() is finding the Artist objects for the candle face (the green) and for the candle wick (the grey). (Also, I am guessing, not sure, that the reason the candle face legend is showing up as green instead of red is because the last candle is green; I'm sure you could experiment with your data to see if this is correct).

I have, in the past, spent some time looking into supporting legends in mplfinance. It seems to be that generally one does not want legends for the candles themselves, but only for moving averages, and/or for make_addplot() calls.

There is a PR (#277) for something along these lines, but upon testing it I found it incomplete and not correctly handling all cases, so for now I have not merged the code. When I, or someone else, has time to go through it and make the code more complete, handling all make_addplot() cases cleanly, and moving averages, then we can move forward on that. (Please let me know if you are interested in contributing).

All the best.

P.S. I have posted to discourse.matplotlib.org to see if there is an easy way around this.

@Santunav
Copy link
Author

Santunav commented Jan 31, 2022

Hi @DanielGoldfarb

Your post on at posted to discourse.matplotlib.org , response from the other forum helped.

mav_titles = ['_not wanted','_not wanted','9 SMA' ,'50 SMA', '200 SMA' ]

Solving issue with a user warning message.

test.py:11: UserWarning: The handle <matplotlib.collections.LineCollection object at 0x7f692ca5eee0> has a label of '_not wanted' which cannot be automatically added to the legend.
axes[0].legend(mav_titles)
test.py:11: UserWarning: The handle <matplotlib.collections.PolyCollection object at 0x7f692ca4f070> has a label of '_not wanted' which cannot be automatically added to the legend.
axes[0].legend(mav_titles)

Thank you
TCS

@Santunav
Copy link
Author

As per https://matplotlib.org/stable/tutorials/intermediate/legend_guide.html

"Those artists with an empty string as label or with a label starting with "_" will be ignored."

@DanielGoldfarb DanielGoldfarb mentioned this issue Jan 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants