Align the Legend Title to the Legend body

Hi, How can I align the Legend Title to the Legend body (variables, etc..)? Maybe left-aligned ?

6 commentaires

Adam Danz
Adam Danz le 14 Sep 2020
Tough one. The title property for legends doesn't have a HorizontalAlignment property nor a position property.
If you add space before the legend title (ie, " my title"), the title will be offset to the right. But if you add space after the title, the space is ignored. I haven't looked any deeper than that, though.
Sim
Sim le 14 Sep 2020
Modifié(e) : Sim le 14 Sep 2020
Thanks a lot for your nice reply Adam :-)
...I tried to add some space already, but it did not work :-)
(However, in such an historical and cool software, I think that a property as the alignment of the legend content should be something quite 'basic'......)
Adam Danz
Adam Danz le 14 Sep 2020
If you contact tech support, they will either suggest a workaround or they may put it on their list of things to improve.
Maybe there's an undocumented method I'm not aware of.
Sim
Sim le 14 Sep 2020
Modifié(e) : Sim le 14 Sep 2020
Thanks a lot! Just submitted to the MATLAB support :)
Adam Danz
Adam Danz le 14 Sep 2020
I'd be interesting in whatever you find.
A possible (not too elegant) workaround is to use the latex interpreter and shift the text manually. E.g.:
fplot(@(x)sin(x),[0,2*pi])
hl = legend('Our quite long legend to see the centering of the title','interpreter','latex');
title(hl,'Legend title \hspace{8 cm}','interpreter','latex')

Connectez-vous pour commenter.

Réponses (1)

Ayush Gupta
Ayush Gupta le 18 Sep 2020
The legend title is by-default left aligned to legend body. Refer to the code below which generates the following picture and read the documentation on how to add legend title, here.
x = linspace(0,pi);
y1 = cos(x);
plot(x,y1)
hold on
y2 = cos(2*x);
plot(x,y2)
hold off
lgd = legend('cos(x)','cos(2x)');
title(lgd,'My Legend Title')

9 commentaires

Adam Danz
Adam Danz le 18 Sep 2020
I don't have access to Matlab at the moment but I'm fairly certain the default alignment is center for legend titles.
If you add space to the beginning of the title you can offset it rightward but if you add space to the right of the title the spaces are trimmed and the title remains centered. At least this is what I remember finding when I was playing around with this in r2019b.
Sim
Sim le 18 Sep 2020
Modifié(e) : Sim le 18 Sep 2020
Thanks Ayush,
I was aware about the default alignement. However, try to get something similar to the text I wrote in my example (with relatevily long texts) and you will find out that legend title and body will lose the alignment.
Just for your knowledge, I opened a ticket to the MATLAB support service with "technical support case" #04491718 regarding 'Suggestion for an improvement'.
@Adam: I experienced what you described in R2019b.....
Adam Danz
Adam Danz le 20 Sep 2020
@Sim
I just saw that as of r2020b you can control the justification of titles but I haven't tested whether or not this works with legend titles.
Sim
Sim le 23 Sep 2020
Modifié(e) : Sim le 23 Sep 2020
Oh thanks a lot, I will try to download r2020b and check that feature :)
Btw, in case it work I should accept your answer, but there is not any button for it (its just besides Ayhus Gupta)
Adam Danz
Adam Danz le 23 Sep 2020
I've checked since I made that comment and legend titles are not supported with the updated features.
In fact, I wrote a Community Highlights about these new features.
Title alignment is controlled by the new "TitleHorizontalAlignment" property of axes. Legends still do not have such a property.
Sim
Sim le 23 Sep 2020
Oh cool, thanks a lot for highlighting that feature... So, if not working for the Legends, my MATLAB ticket opening was worth it :)
Sim
Sim le 17 Août 2026 à 14:35
Hi @Adam Danz, any news about the alignment of legend titles? :-)
Adam Danz
Adam Danz le 17 Août 2026 à 15:39
Hi @Sim, still not possible. I've passed along this request to control horizontal alignment in the legend title. Thanks for following up.
Sim
Sim le 18 Août 2026 à 7:47
Modifié(e) : Sim le 18 Août 2026 à 7:55
Thanks a lot, Adam! :-) Hopefully, someone on the development team will be able to add that feature soon :-)
My current workaround is to add a simple text label as a proxy for the legend title, just above the legend body. Alternatively, I sometimes add a second legend solely to display the title text for the first legend, using a fictitious plot of NaNs, e.g. h = plot(NaN, NaN, ..., 'DisplayName', ...); legend(new_axes, h).

Connectez-vous pour commenter.

Question posée :

Sim
le 14 Sep 2020

Modifié(e) :

Sim
le 18 Août 2026 à 7:55

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by