You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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
Reacted by Adam Anderson, Yilu, dead-lamer, Marcin Zawodnik, shish1976 and Sebastian Dumitrascu
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 jainraje@yahoo.com.
It worked, but only one of the plots can display (see attachment).
I'll need help on why only one of them is showing in the legends.
I debugged it, and i think i know the issue, the legendHandles only gives one Line2D object when printed, this might be a hint that can help us find the problem
It worked, but only one of the plots can display (see attachment).
I'll need help on why only one of them is showing in the legends.
I debugged it, and i think i know the issue, the legendHandles only gives one Line2D object when printed, this might be a hint that can help us find the problem
Thanks for your comments!
Can you share your code related to the figures you showed me?
It would be helpful for me.
Did some edits in the second call of axes[2].legend, in your example, it was supposed to be handles=handles[2:], but if i do that, it just completely removes the legends. I removed the [2:] and it worked, but still, only one is showing.
hi, also need labels help w mplfinance. but you guys lost me per how i could adapt my code to do labels , in a script run in powershell, and save to a file.
previously working code that writes my plots to file but no labels...
i changed it to this, but get
error
cannot unpack non-iterable NoneType object
TypeError('cannot unpack non-iterable NoneType object')
i dont understand how you could do the fig,axes part AFTER you call mpf.plot. like that would have to be before .plot() call which has the savefig= param. i do have 3 panels. candles, stock and macd. but im lost per how to get labels on the plot and save it to a file from a script.
thanks. code i tried per above replies:
you see ax6 , the stochrsi subplot on the bottom....i dont understand how to reference that handles of legend should be the correct colors. its obviuosly giving the colors of the top panels first 2...but no matter what i tried n read in the docs, i couldnt find examples both ref'd panels legend and from a addplot list.
thanks
Have you tried using axes[7]? You have a total of 4 panels (0-3), which means 8 Axes: 0-7
Also, I'm confused by something in your code: The code appears to be putting StochRSI on panel 2, and macd on panel 3. But in the plot image StochRSI is on the bottom panel (panel 3) and MACD is just above that (panel 2).
I would also point out that the StochRSI panel has y-ticks on both the left and right, which suggests that between 'stochrsi_K' and 'stochrsi_D', one of them may be using axes[7] and the other axes[6]. If this is what you want, fine. If not, then you can code specifically secondary_y=False (or True, depending what you want), because by not specifying secondary_y at all, its default value is 'auto' which means that mplfinance may decide to put them on separate Axes.
Also, one other point to keep in mind, that may have an effect:
The "charles" style puts the primary axes on the right (see y_on_right = True inside the "charles" style definition file). This means that secondary_y will be on the left. Thus Axes 0,2,4,6 have their y-values on the right, while Axes 1,3,5,7 are have their y-values on the left.
Keep in mind that if you don't specify secondary_y (thus defaulting to 'auto') then the first data plotted on each panel will go on the primary axes (0,2,4,6) while any subsequent data may be plotted on the on either the primary (0,2,4,6) or the secondary (1,3,5,7) depending on what mplfinance decides. You can force it to be on primary or secondary by specifying secondary_y=False (primary) or secondary_y=True (secondary).
YES , thanks, my mistake flipping the labels and secondary_y all being true. fixed that!
and i got the stochrsi colors to be correct in the legend now :D pulling into handles4 and referencing handles4 made it work!
try:
axes[4].legend([None]*(len(ap3)+2))
handles4 = axes[4].get_legend().legendHandles
axes[4].legend(handles=handles4, labels=['stochrsi_K', 'stochrsi_D'], loc='center left', fontsize='x-small')
except Exception as e:
print('error making legend axis4')
print(e)
Also, one other point to keep in mind, that may have an effect: The "charles" style puts the primary axes on the right (see y_on_right = True inside the "charles" style definition file). This means that secondary_y will be on the left. Thus Axes 0,2,4,6 have their y-values on the right, while Axes 1,3,5,7 are have their y-values on the left.
Keep in mind that if you don't specify secondary_y (thus defaulting to 'auto') then the first data plotted on each panel will go on the primary axes (0,2,4,6) while any subsequent data may be plotted on the on either the primary (0,2,4,6) or the secondary (1,3,5,7) depending on what mplfinance decides. You can force it to be on primary or secondary by specifying secondary_y=False (primary) or secondary_y=True (secondary).
THANKS and hope the mulitpanel legend struggle of mine helps people
.
THANKS and hope the mulitpanel legend struggle of mine helps people
There is no question. Posting your struggles here definitely helps other mplfinance users.
Not only does it help them directly, but it will help them indirectly in the future, as it is my intention to implement a legends kwarg to mplfinance.plot() hopefully before the end of this year. All of the posts in this issue will certainly help guide that implementation in order to make the legends kwarg of mplfinace both as simple to use, and as flexible, as possible.
Two addition things:
Looking at your last plot it appears that "MACDh" in the legend somehow picked up green instead of blue.
I can't see why because the code is not posted, which leads me to my second item:
Would you mind posting your final complete code here for all to see? That will be most helpful and very appreciated by your fellow mplfinance users.
Finally, let me just say that your plot is one of the best mplfinance plots I have ever seen! Really, excellent job! Thank you for sharing!
No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when legend() is called with no argument.
Couldn't find what is the cause of error. Can anybody help?
Ps: I can see drawing but no legends
Support for legends is a very important capability for mplfinance to directly support. In the meantime, I really liked the sample code from Youngil Koh and it allowed me to succeed in my efforts to get proper legend display.
My application is a bit complex and the addplots are conditionally created. To avoid rewriting it to conform to the example code, I decided to get the labels out of the addplot data structures. Here is the resulting code snippet and function which I created for my application. It creates the legend for the main panel (panel 0) or any of the sub panels.
def plotter(someargs):
....
style = mpf.make_mpf_style(gridcolor="gray", gridstyle="--", gridaxis="both", facecolor='0.98', figcolor='0.93',
marketcolors=mpf.make_marketcolors(volume={'up':'tab:green', 'down':'tab:red'}, vcdopcod=True))
fig, axes = mpf.plot(df, type=ptype, volume=volume_requested, figscale=2, title=title, tight_layout=True,
panel_ratios=panel_ratios, addplot=ap_list, scale_width_adjustment=dict(lines=0.9, volume=0.9),
linecolor='black', style=style, returnfig=True)
# add a legend to the main plot window and some subpanels
add_legend(axes, ap_list, 0, ptype=ptype)
for panel in legend_needed:
add_legend(axes, ap_list, panel)
....
def add_legend(axes, add_plots, panel, primary_label='', ptype=None, loc='upper left'):
labels = []
# set label for main panel primary data series
if panel == 0:
if primary_label == '':
primary_label = 'AdjOHLC' if ptype == 'ohlc' or ptype == 'candle' else 'AdjClose'
labels.append(primary_label)
# grab labels from the add plots attached to the specified panel
for ap_data in [ap['data'] for ap in add_plots if ap['panel'] == panel]:
if isinstance(ap_data, pd.Series):
labels.append(ap_data.name)
elif isinstance(ap_data, pd.DataFrame):
labels += list(ap_data.columns)
# need to use offsets to get correct ax and handles based on figure structure
ax_offset = 2 if panel > 0 else 0
handle_offset = 2 if ptype == 'ohlc' else 1 if ptype == 'candle' else 0
# add the legend
axes[panel + ax_offset].legend([None] * (len(labels) + handle_offset))
handles = axes[panel + ax_offset].get_legend().legendHandles
axes[panel + ax_offset].legend(handles=handles[handle_offset:], labels=labels, loc=loc)
you see ax6 , the stochrsi subplot on the bottom....i dont understand how to reference that handles of legend should be the correct colors. its obviuosly giving the colors of the top panels first 2...but no matter what i tried n read in the docs, i couldnt find examples both ref'd panels legend and from a addplot list. thanks
wow! How do you get the candles etc so crips in your image from the mplfinance save file? would love mine to look like this.
As an example, here's what my candles look like. not bad but it's not the view that i want and struggling to make it look crisper and larger in context of the overall saved image. also want to add the legend.
Heres the code im using to save the file but it's still not quite right: mpf.plot(data, type='candle', style='yahoo', title=title,savefig=dict(fname=savedPlotFileName,dpi=400,pad_inches=0.0), hlines=dict(hlines=[fib_62, fib_705, fib_79,fib_21, fib_295, fib_38],colors=['r','g','r','r','g','r'],linestyle='-.'), fill_between=dict(y1=minlow,y2=minhigh,where=where_values, alpha=0.5, color='g'))
Is there any updates on the LEGENDS capability within the package yet?
You need to get the handles from the same subplot where you are intending to define the legend. In your code you have:
handles = axes[0].get_legend().legendHandles
which you use for the main plot, but when moving on to the subplots, you did not grab the handles again from those axes...
On Jun 25, 2023, at 5:32 PM, TraderMan999 ***@***.***> wrote:
this helped BUT how do i get proper colors on other panels ....lost a whole day experimenting , no luck. thanks in advance
The problem, I think, is in this line of code:
fig, axes = mpf.plot(df.tail(200), type='candle', volume=True, mav=(26, 50, 75, 100, 150),
addplot=list(ap2.values()), style='charles', savefig=save)
change savefig to returnfig=True :
fig, axes = mpf.plot(df.tail(200), type='candle', volume=True, mav=(26, 50, 75, 100, 150),
addplot=list(ap2.values()), style='charles', returnfig=True)
ap3 = {
"SuperTrend_Long": mpf.make_addplot(df['SUPERTl'].tail(tail_num), color='g'),
"SuperTrend_Short": mpf.make_addplot(df['SUPERTs'].tail(tail_num), color='r'),
"PSAR_Long": mpf.make_addplot(df['PSARl'].tail(tail_num), color='y'),
"PSAR_Short": mpf.make_addplot(df['PSARs'].tail(tail_num), color='y'),
"EMA_15": mpf.make_addplot(df['EMA_15'].tail(tail_num), color='#55'),
"EMA_20": mpf.make_addplot(df['EMA_20'].tail(tail_num), color='#99'),
"EMA_30": mpf.make_addplot(df['EMA_30'].tail(tail_num), color='#0000CC'),
"EMA_50": mpf.make_addplot(df['EMA_50'].tail(tail_num), color='#005500'),
"EMA_75": mpf.make_addplot(df['EMA_75'].tail(tail_num), color='#00CC00'),
"EMA_100": mpf.make_addplot(df['EMA_100'].tail(tail_num), color='#990000'),
"EMA_200": mpf.make_addplot(df['EMA_200'].tail(tail_num), color='#CC0000'),
"stochrsi_K": mpf.make_addplot(df['stochrsi_K_ultragtx'].tail(tail_num), color='g', panel=2), # panel 2 specified
"stochrsi_D": mpf.make_addplot(df['stochrsi_D_ultragtx'].tail(tail_num), color='b', panel=2), # panel 2 specified
"MACDh": mpf.make_addplot(df['MACDh_6_12_9'].tail(tail_num), color='r', secondary_y=True, panel=3),
"MACDs": mpf.make_addplot(df['MACDs_6_12_9'].tail(tail_num), color='b', secondary_y=True, panel=3)
}
print('making axes0 legend')
fig, axes = mpf.plot(df.tail(tail_num), type='candle', volume=True, addplot=list(ap3.values()), style='charles',
returnfig=True, title=('Last '+str(tail_num)+' candles'+fname), scale_width_adjustment=dict(lines=0.25))
axes[0].legend([None]*(len(ap3)+2))
handles = axes[0].get_legend().legendHandles
axes[0].legend(handles=handles[2:], labels=list(ap3.keys()), loc='upper left', fontsize='x-small')
#axes[0].legend(loc='upper left', fontsize='x-small')
# IPydisplay.Image(ourpath)
print(axes)
axes[4].set_ylabel("MACD")
axes[6].set_ylabel("StochRSI")
print(axes)
print(len(ap3))
try:
axes[4].legend([None]*(len(ap3)+2))
axes[4].legend(handles=handles[2:], labels=['MACDh', 'MACDs'], loc='center left', fontsize='x-small')
except Exception as e:
print('error making legend axis4')
print(e)
print('making axes6 legend')
try:
# axes[6].legend([None]*(len(ap3)+2))
# axes[6].legend([None]*(2+2))
#handles6 = axes[6].get_legend().legendHandles
axes[6].legend(handles=handles[2:], labels=['stochrsi_K', 'stochrsi_D'], loc='center left', fontsize='x-small')
except Exception as e:
print('error making legend axis6')
print(e)
fig.savefig('plots/'+fname, dpi=300)
<https://user-images.githubusercontent.com/97939226/175432610-9a387d8d-42ea-465c-83b5-b688073dffa7.png>
you see ax6 , the stochrsi subplot on the bottom....i dont understand how to reference that handles of legend should be the correct colors. its obviuosly giving the colors of the top panels first 2...but no matter what i tried n read in the docs, i couldnt find examples both ref'd panels legend and from a addplot list. thanks
wow! How do you get the candles etc so crips in your image from the mplfinance save file? would love mine to look like this.
As an example, here's what my candles look like. not bad but it's not the view that i want and struggling to make it look crisper and larger in context of the overall saved image. also want to add the legend.
https://ibb.co/g6cVcy8 <https://ibb.co/g6cVcy8>
Is there any updates on the LEGENDS capability within the package yet?
—
Reply to this email directly, view it on GitHub <#181 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AD4NKOSLOV2EY6YFEQRXGELXNC4AXANCNFSM4OCESKDQ>.
You are receiving this because you commented.
You need to get the handles from the same subplot where you are intending to define the legend. In your code you have: handles = axes[0].get_legend().legendHandles which you use for the main plot, but when moving on to the subplots, you did not grab the handles again from those axes... …
On Jun 25, 2023, at 5:32 PM, TraderMan999 @.> wrote: this helped BUT how do i get proper colors on other panels ....lost a whole day experimenting , no luck. thanks in advance The problem, I think, is in this line of code: fig, axes = mpf.plot(df.tail(200), type='candle', volume=True, mav=(26, 50, 75, 100, 150), addplot=list(ap2.values()), style='charles', savefig=save) change savefig to returnfig=True : fig, axes = mpf.plot(df.tail(200), type='candle', volume=True, mav=(26, 50, 75, 100, 150), addplot=list(ap2.values()), style='charles', returnfig=True) ap3 = { "SuperTrend_Long": mpf.make_addplot(df['SUPERTl'].tail(tail_num), color='g'), "SuperTrend_Short": mpf.make_addplot(df['SUPERTs'].tail(tail_num), color='r'), "PSAR_Long": mpf.make_addplot(df['PSARl'].tail(tail_num), color='y'), "PSAR_Short": mpf.make_addplot(df['PSARs'].tail(tail_num), color='y'), "EMA_15": mpf.make_addplot(df['EMA_15'].tail(tail_num), color='#55'), "EMA_20": mpf.make_addplot(df['EMA_20'].tail(tail_num), color='#99'), "EMA_30": mpf.make_addplot(df['EMA_30'].tail(tail_num), color='#0000CC'), "EMA_50": mpf.make_addplot(df['EMA_50'].tail(tail_num), color='#005500'), "EMA_75": mpf.make_addplot(df['EMA_75'].tail(tail_num), color='#00CC00'), "EMA_100": mpf.make_addplot(df['EMA_100'].tail(tail_num), color='#990000'), "EMA_200": mpf.make_addplot(df['EMA_200'].tail(tail_num), color='#CC0000'), "stochrsi_K": mpf.make_addplot(df['stochrsi_K_ultragtx'].tail(tail_num), color='g', panel=2), # panel 2 specified "stochrsi_D": mpf.make_addplot(df['stochrsi_D_ultragtx'].tail(tail_num), color='b', panel=2), # panel 2 specified "MACDh": mpf.make_addplot(df['MACDh_6_12_9'].tail(tail_num), color='r', secondary_y=True, panel=3), "MACDs": mpf.make_addplot(df['MACDs_6_12_9'].tail(tail_num), color='b', secondary_y=True, panel=3) } print('making axes0 legend') fig, axes = mpf.plot(df.tail(tail_num), type='candle', volume=True, addplot=list(ap3.values()), style='charles', returnfig=True, title=('Last '+str(tail_num)+' candles'+fname), scale_width_adjustment=dict(lines=0.25)) axes[0].legend([None](len(ap3)+2)) handles = axes[0].get_legend().legendHandles axes[0].legend(handles=handles[2:], labels=list(ap3.keys()), loc='upper left', fontsize='x-small') #axes[0].legend(loc='upper left', fontsize='x-small') # IPydisplay.Image(ourpath) print(axes) axes[4].set_ylabel("MACD") axes[6].set_ylabel("StochRSI") print(axes) print(len(ap3)) try: axes[4].legend([None](len(ap3)+2)) axes[4].legend(handles=handles[2:], labels=['MACDh', 'MACDs'], loc='center left', fontsize='x-small') except Exception as e: print('error making legend axis4') print(e) print('making axes6 legend') try: # axes[6].legend([None](len(ap3)+2)) # axes[6].legend([None]*(2+2)) #handles6 = axes[6].get_legend().legendHandles axes[6].legend(handles=handles[2:], labels=['stochrsi_K', 'stochrsi_D'], loc='center left', fontsize='x-small') except Exception as e: print('error making legend axis6') print(e) fig.savefig('plots/'+fname, dpi=300) https://user-images.githubusercontent.com/97939226/175432610-9a387d8d-42ea-465c-83b5-b688073dffa7.png you see ax6 , the stochrsi subplot on the bottom....i dont understand how to reference that handles of legend should be the correct colors. its obviuosly giving the colors of the top panels first 2...but no matter what i tried n read in the docs, i couldnt find examples both ref'd panels legend and from a addplot list. thanks wow! How do you get the candles etc so crips in your image from the mplfinance save file? would love mine to look like this. As an example, here's what my candles look like. not bad but it's not the view that i want and struggling to make it look crisper and larger in context of the overall saved image. also want to add the legend. https://ibb.co/g6cVcy8https://ibb.co/g6cVcy8 Is there any updates on the LEGENDS capability within the package yet? — Reply to this email directly, view it on GitHub <#181 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AD4NKOSLOV2EY6YFEQRXGELXNC4AXANCNFSM4OCESKDQ. You are receiving this because you commented.
i think youre responding to the code i copied from someone else
Can anyone suggest what i can do different to make these candles appear relatively bigger on this image? ive spent hours changing the various figsize parameters, dpi, figscale, etc to no avail. Here's some sample code.
There are a few things you can try, lsited here in the order of what I suspect is most likely to help:
For sure getting rid of the lowerhlines and possibly setting ylim=(4075,4175) will help. By the way, the code you posted doesn't seem to match the plot image that you posted.
There are a few things you can try, lsited here in the order of what I suspect is most likely to help:
For sure getting rid of the lowerhlines and possibly setting ylim=(4075,4175) will help. By the way, the code you posted doesn't seem to match the plot image that you posted.
scale_width_adjustment=dict(candle=1.35) (play with various values to see what works best)
Play around with various figscale= values. Sometimes making this larger, or even smaller, can improve candle appearance.
Similarly playing with different values of dpi may (or may not) help.
thanks let me give that a try.
weird i think youre right. i must have made a copy / paste error. let me post below for clarification oops i see what i did. i grabbed the wrong image url for the post. ive posted the correct one here below
The lower hlines are fibs im tracking but ive pondered the value of having them there when the actual candles are so far from them so that could also be a compromise with a little if then else logic i can add
AND AS ALWAYS THANK YOU FOR THE QUICK RESPONSE. I LOVE HOW ENGAGED YOU ARE IN YOUR PRODUCT FOR YOUR USER COMMUNITY. ONE OF THE BEST!
Activity
DanielGoldfarb commentedon Jun 18, 2020
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=Truekwarg 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
jainraje commentedon Jun 21, 2020
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 jainraje@yahoo.com.
All the best
--Rajeev
arthurpdesimone commentedon Jul 31, 2020
Plot chart to figure and axis object
This does the trick thanks to the help of mr. Daniel Goldfarb.
DanielGoldfarb commentedon Jul 31, 2020
@arthurpdesimone
Thank you for posting a specific example to help others. Much appreciated. All the best. --Daniel
YoungilKoh commentedon Mar 15, 2022
Hi, I'm Youngil Koh
I think the sample code below would be helpful.
DanielGoldfarb commentedon Mar 15, 2022
@YoungilKoh
Thank you so much! That is an excellent example! Much appreciated.
BlackSharkJ commentedon Apr 18, 2022
Thank you for your help. You're amazing
quebeh commentedon May 2, 2022
I found an issue while following @YoungilKoh 's answer (#181 (comment))
It worked, but only one of the plots can display (see attachment).
I'll need help on why only one of them is showing in the legends.
I debugged it, and i think i know the issue, the legendHandles only gives one Line2D object when printed, this might be a hint that can help us find the problem
YoungilKoh commentedon May 2, 2022
Thanks for your comments!
Can you share your code related to the figures you showed me?
It would be helpful for me.
quebeh commentedon May 2, 2022
Here's my code:
Did some edits in the second call of
axes[2].legend, in your example, it was supposed to behandles=handles[2:], but if i do that, it just completely removes the legends. I removed the[2:]and it worked, but still, only one is showing.Thank you for helping.
quebeh commentedon May 6, 2022
Well out of nowhere, it just got fixed, i think its a new update, i used the same method. Thanks!
cryptoliciousdev commentedon Jun 21, 2022
hi, also need labels help w mplfinance. but you guys lost me per how i could adapt my code to do labels , in a script run in powershell, and save to a file.
previously working code that writes my plots to file but no labels...
i changed it to this, but get
error
cannot unpack non-iterable NoneType object
TypeError('cannot unpack non-iterable NoneType object')
i dont understand how you could do the fig,axes part AFTER you call mpf.plot. like that would have to be before .plot() call which has the savefig= param. i do have 3 panels. candles, stock and macd. but im lost per how to get labels on the plot and save it to a file from a script.
thanks. code i tried per above replies:
DanielGoldfarb commentedon Jun 21, 2022
The problem, I think, is in this line of code:
change
savefigtoreturnfig=True:cryptoliciousdev commentedon Jun 23, 2022
this helped BUT how do i get proper colors on other panels ....lost a whole day experimenting , no luck. thanks in advance
ap3 = {
you see ax6 , the stochrsi subplot on the bottom....i dont understand how to reference that handles of legend should be the correct colors. its obviuosly giving the colors of the top panels first 2...but no matter what i tried n read in the docs, i couldnt find examples both ref'd panels legend and from a addplot list.
thanks
DanielGoldfarb commentedon Jun 24, 2022
Have you tried using
axes[7]? You have a total of 4 panels (0-3), which means 8 Axes:0-7Also, I'm confused by something in your code: The code appears to be putting StochRSI on panel 2, and macd on panel 3. But in the plot image StochRSI is on the bottom panel (panel 3) and MACD is just above that (panel 2).
I would also point out that the StochRSI panel has y-ticks on both the left and right, which suggests that between 'stochrsi_K' and 'stochrsi_D', one of them may be using axes[7] and the other axes[6]. If this is what you want, fine. If not, then you can code specifically
secondary_y=False(orTrue, depending what you want), because by not specifyingsecondary_yat all, its default value is'auto'which means that mplfinance may decide to put them on separate Axes.DanielGoldfarb commentedon Jun 24, 2022
Also, one other point to keep in mind, that may have an effect:
The "charles" style puts the primary axes on the right (see
y_on_right = Trueinside the "charles" style definition file). This means thatsecondary_ywill be on the left. Thus Axes 0,2,4,6 have their y-values on the right, while Axes 1,3,5,7 are have their y-values on the left.Keep in mind that if you don't specify
secondary_y(thus defaulting to'auto') then the first data plotted on each panel will go on the primary axes (0,2,4,6) while any subsequent data may be plotted on the on either the primary (0,2,4,6) or the secondary (1,3,5,7) depending on what mplfinance decides. You can force it to be on primary or secondary by specifyingsecondary_y=False(primary) orsecondary_y=True(secondary).cryptoliciousdev commentedon Jun 24, 2022
YES , thanks, my mistake flipping the labels and secondary_y all being true. fixed that!
and i got the stochrsi colors to be correct in the legend now :D pulling into handles4 and referencing handles4 made it work!
THANKS and hope the mulitpanel legend struggle of mine helps people

.
DanielGoldfarb commentedon Jun 24, 2022
There is no question. Posting your struggles here definitely helps other mplfinance users.
Not only does it help them directly, but it will help them indirectly in the future, as it is my intention to implement a
legendskwarg tomplfinance.plot()hopefully before the end of this year. All of the posts in this issue will certainly help guide that implementation in order to make thelegendskwarg of mplfinace both as simple to use, and as flexible, as possible.Two addition things:
I can't see why because the code is not posted, which leads me to my second item:
Finally, let me just say that your plot is one of the best mplfinance plots I have ever seen! Really, excellent job! Thank you for sharing!
All the best. --Daniel
drdoof2019 commentedon Jul 29, 2022
This gives error:
Couldn't find what is the cause of error. Can anybody help?
Ps: I can see drawing but no legends
ashraf2000shweky commentedon Dec 29, 2022
with subplots it's easy👍👍
fig , ax =plt.subplots(3,1,sharex=True,figsize=(10,10),gridspec_kw= {'height_ratios':[3,2,2]})
fig.set_edgecolor('k')
fig.set_linewidth(4)
fig.set_facecolor('cyan')
ax3 = ax[2].twinx()
p2 = [ mpf.make_addplot(data['High'],color='g',ax=ax[2]),
mpf.make_addplot(data['Low'],color='b',ax=ax[2]),
mpf.make_addplot(data['macd'],ax=ax3,color='r')]
mpf.plot(data,type='candle',ax=ax[0], addplot=p2 ,style='blueskies',volume=ax[1],mav=(6,9))
fig.suptitle('AAPL')
ax[0].legend(labels=['nolegend','nolegend','mav6','mav9'])
ax[2].legend(['High','Low'])
ax3.legend(['MACD'],bbox_to_anchor=[0.7,0.2])
ax[2].grid(False)
ax[0].set_ylabel('Price',fontsize=15,labelpad=30,color='r')
ax[1].set_ylabel('Volume',fontsize=10,labelpad=35,color='r')
plt.tight_layout()
plt.show()
DavidT-123 commentedon May 21, 2023
Support for legends is a very important capability for mplfinance to directly support. In the meantime, I really liked the sample code from Youngil Koh and it allowed me to succeed in my efforts to get proper legend display.
My application is a bit complex and the addplots are conditionally created. To avoid rewriting it to conform to the example code, I decided to get the labels out of the addplot data structures. Here is the resulting code snippet and function which I created for my application. It creates the legend for the main panel (panel 0) or any of the sub panels.
TraderMan999 commentedon Jun 25, 2023
wow! How do you get the candles etc so crips in your image from the mplfinance save file? would love mine to look like this.
As an example, here's what my candles look like. not bad but it's not the view that i want and struggling to make it look crisper and larger in context of the overall saved image. also want to add the legend.
https://ibb.co/g6cVcy8
Heres the code im using to save the file but it's still not quite right:
mpf.plot(data, type='candle', style='yahoo', title=title,savefig=dict(fname=savedPlotFileName,dpi=400,pad_inches=0.0), hlines=dict(hlines=[fib_62, fib_705, fib_79,fib_21, fib_295, fib_38],colors=['r','g','r','r','g','r'],linestyle='-.'), fill_between=dict(y1=minlow,y2=minhigh,where=where_values, alpha=0.5, color='g'))Is there any updates on the LEGENDS capability within the package yet?
DavidT-123 commentedon Jun 26, 2023
TraderMan999 commentedon Jun 26, 2023
i think youre responding to the code i copied from someone else
TraderMan999 commentedon Jun 26, 2023
https://ibb.co/g6cVcy8
Can anyone suggest what i can do different to make these candles appear relatively bigger on this image? ive spent hours changing the various figsize parameters, dpi, figscale, etc to no avail. Here's some sample code.
DanielGoldfarb commentedon Jun 26, 2023
There are a few things you can try, lsited here in the order of what I suspect is most likely to help:
hlinesand possibly settingylim=(4075,4175)will help. By the way, the code you posted doesn't seem to match the plot image that you posted.scale_width_adjustment=dict(candle=1.35)(play with various values to see what works best)figscale=values. Sometimes making this larger, or even smaller, can improve candle appearance.dpimay (or may not) help.TraderMan999 commentedon Jun 27, 2023
thanks let me give that a try.
weird i think youre right. i must have made a copy / paste error. let me post below for clarification
oops i see what i did. i grabbed the wrong image url for the post. ive posted the correct one here below
https://ibb.co/NK0GYCq
The lower hlines are fibs im tracking but ive pondered the value of having them there when the actual candles are so far from them so that could also be a compromise with a little if then else logic i can add
AND AS ALWAYS THANK YOU FOR THE QUICK RESPONSE. I LOVE HOW ENGAGED YOU ARE IN YOUR PRODUCT FOR YOUR USER COMMUNITY. ONE OF THE BEST!
TraderMan999 commentedon Jun 27, 2023
The ylim definitely helped. i can probably live with that. thanks for that suggestion