MATLAB Answers

How to enlarge legend marker size in r2014b?

35 views (last 30 days)
Michelle Tadmor
Michelle Tadmor on 20 Nov 2014
Commented: Nike Dattani on 18 Nov 2018
In previous versions one could grab the children of the legend object to change their size individually but now those don't exist anymore. How can I present larger markers in the legend that correspond to tiny scatter dots?


Sign in to comment.

Answers (2)

Kelly Kearney
Kelly Kearney on 20 Nov 2014
The marker handles are still there...
load seamount;
hs = scatter(x,y,5,z);
[hleg, hobj, hout, mout] = legend(hs, 'test');
hobj(2).Children.MarkerSize = 10;


Show 1 older comment
Kelly Kearney
Kelly Kearney on 20 Nov 2014
Odd... I'm seeing different results. Running 2014b on Mac OS X 10.9.3:
>> load seamount;
>> hs = scatter(x,y,5,z);
>> [hleg, hobj, hout, mout] = legend(hs, 'test');
>> hobj
hobj =
2x1 graphics array:
Text (test)
Group (test)
>> hobj(2).Children
ans =
Patch with properties:
FaceColor: 'none'
FaceAlpha: 1
EdgeColor: 'none'
LineStyle: '-'
Faces: 1
Vertices: [0.37164 0.5]
Show all properties
Without making any changes, the MarkerSize seems to be set to 6, and appears larger than the scatter objects:
Are you sure you were referencing hobj, and not hleg.Children? Because it's true that the legend objects are no longer children of the legend itself, as they were in previous versions, but you can still access those handles via the second output to the legend function. The third output, hout, references the labeled objects (so in this case hout and hs are the same), which is why changing the size changed your scatter plot markers.
If I change the MarkerSize property of hobj(2).Children, it enlarges the legend symbol on my screen
hobj(2).Children.MarkerSize = 20;
Though on a side note, I had to get that second figure via a screen capture, because the legend symbol size reverts to its original when I try to save via the usual methods (tried export_fig, print, and the "Save As" menu option).
Kelly Kearney
Kelly Kearney on 21 Nov 2014
I've tracked the reverting-on-export issue to line 215 in print.m, which calls the private function alternatePrintPath.p... not quite sure what goes on under the hood there, but it seems to relink the legend to its labeled objects.
If you use my legendflex function instead, you can get the proper export, since the new "legend" isn't a real legend object and therefore decouples the label from the original data object:
hs = scatter(x,y,5,z);
[hleg, hobj, hout, mout] = legendflex(hs, {'test'});
set(get(hobj(2), 'Children'), 'markersize', 20);
(Though I'm a little behind in getting that function thoroughly R2014b-compatible. It works for most cases, but I still have some numeric handles left over that should be converted to graphics objects... I hope to get that cleaned up soon).
P.S. In response to Star, writing and maintaining legendflex.m is how I've learned my way around the inner workings of legends. I can't take credit for the bar offset property; I found this by the master of all things undocumented in another Answer.
Star Strider
Star Strider on 21 Nov 2014
Thanks for that link, Kelly. I’d not discovered it.
I’m going to keep R2014a installed in the event I actually need to do something!
R2014b is frustrating in its many limitations. (Among its other problems, the datetime functions cannot be used with surface plots. At least in some instances, setting options to override defaults doesn’t automatically switch the associated ‘Mode’ to 'manual'. That has to be done in a separate command in order to get the override to work.)

Sign in to comment.

Romesh on 26 Nov 2017
Confirming on R2017a that the second output of legend has the necessary handles e.g.
[~,b] = legend('foo','bar','baz')
K>> b
b =
6×1 graphics array:
Text (a)
Text (b)
Text (c)
Group (a)
Group (b)
Group (c)
However, they are nested within the Group objects, and they are also a mixture of line and patch objects. An easy way to change the size then is to use findobj() to retrieve all of the handles with the MarkerSize property, and then to set them i.e.


Nike Dattani
Nike Dattani on 18 Nov 2018
This works for making the marker size bigger in the legend, but then the text is not interpreted by LaTeX, instead it's interpreted by TeX, and even changing it manually (by right clicking on the actual figure, for example), it does not get interpreted by LaTeX!
Nike Dattani
Nike Dattani on 18 Nov 2018
The font size also seems not to change :'(

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!