Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Marker/line attributes in legend vs. corresponding plot

1,163 views
Skip to first unread message

CC

unread,
Dec 10, 2008, 9:52:02 PM12/10/08
to
On a plot with a legend, how can I get the markers in the legend to be a different size from the one in the plots that the legend is pointing to? I want to do this since my plot markers are small due to the large amount of data, but then they don't show up very well in the legend.

Example code:
x=[1:1:10];
figure;
hplot = plot(x,x,'.k');
h_for_legend = hplot;
set(hplot,'MarkerSize',2); % small size marker
set(h_for_legend,'MarkerSize',12); % attempt to get larger marker for legend
legend_string = 'my data';
legend(h_for_legend,legend_string);

I was hoping that the markers on the plot (handle hplot) would be size 2, and the marker in the legend (handle h_for_legend) would be size 12 so it would be visible. A single size 2 marker in the legend is too small. But the datapoints in the plot inheirit the marker size that I gave the legend (12). How can I get them to be different in the plot vs. the legend (and change other attributes as well if I want)?

Thanks very much.

Jimy

unread,
Dec 10, 2008, 11:10:04 PM12/10/08
to
"CC " <omaha8...@yahoo.com> wrote in message <ghpv8i$qps$1...@fred.mathworks.com>...

Hi,

One possible way of changing the 'MarkerSize' of the legend would be as follows:


x=[1:1:10];
figure;
hplot = plot(x,x,'.k');

set(hplot,'MarkerSize',10);
legend_string = 'my data';
%%NEW
l=legend(hplot,legend_string);
%l=findobj(gcf,'tag','legend') % Alternate way of finding the legend handle if not
% known in advance.
a=get(l,'children');
%a(1) corresponds to the marker object
set(a(1),'markersize',20); % This line changes the legend marker size
%%NEW

You can do a GET on the legend handle or on other objects of legend such as 'text', marker etc. to get more information on available properties of each of these objects.

I hope this helps !

-Jimy

CC

unread,
Dec 13, 2008, 2:23:06 AM12/13/08
to
The code works, however how can I see what the objects are in the legend children (i.e. variable a, in your code)? That is, how do I know that a(1) is the marker object, and how can I identify the other attributes of the legend children which I may want to change?

Thanks very much.




a=get(l,'children');
%a(1) corresponds to the marker object
set(a(1),'markersize',20); % This line changes the legend marker size
%%NEW

"Jimy " <jimy...@mathworks.com> wrote in message <ghq3qs$q36$1...@fred.mathworks.com>...

Zereen

unread,
Mar 23, 2012, 11:37:29 AM3/23/12
to
"Jimy Shah" wrote in message <ghq3qs$q36$1...@fred.mathworks.com>...
Hi Jimy,

I was facing the similar problem and I tried your suggestion. However, it only changes the size of the last marker not all of them. Can you please help me with this?

Thanks,
Zereen

Ioanna

unread,
Jun 12, 2012, 1:08:07 PM6/12/12
to
"Zereen" wrote in message <jki5bp$3i5$1...@newscl01ah.mathworks.com>...
>
> Hi Jimy,
>
> I was facing the similar problem and I tried your suggestion. However, it only changes the size of the last marker not all of them. Can you please help me with this?
>
> Thanks,
> Zereen

You need to replace the set command with
set(a([1:3:end]), 'MarkerSize', 20);
a(1) is the handle of only the last marker. a(4) is the second to last, etc.

Tierney

unread,
Aug 13, 2013, 9:55:08 AM8/13/13
to
Try this link! It worked for me where as the code in this thread did not.

% Here is this code from the link below.
M = findobj(h,'type','patch') % Find objects of type 'patch'
set(M,'MarkerSize', sqrt(150)) %Calculate marker size based on size of scatter points

http://www.mathworks.com/support/solutions/en/data/1-AZNG9O/?product=SL&solution=1-AZNG9O

deus dionyssos

unread,
Aug 29, 2013, 11:35:05 AM8/29/13
to
hold on
plot(NaN,'.','markersize',70); % dummy plot (NaN's do not appear)
legend('whatever');
plot(1,1,'marker','.','markersize',17); % the usefull data plot
hold off

Elizabeth

unread,
Oct 30, 2013, 4:16:06 PM10/30/13
to
I tried the below solution, however I received an error message:

??? Error using ==> set
Invalid property found.
Object Name : text
Property Name : 'MarkerSize'.

I am using R2010b maci64.

Thanks,

L.

Elizabeth

unread,
Oct 30, 2013, 4:36:06 PM10/30/13
to
Nevermind, I figured it out. I had not seen that one needs to use an interval of 3 a(1:3:end) to pick out the markers in the legend.

Thanks this worked perfectly for me!

Liz.


"Elizabeth " <ea...@unh.edu> wrote in message <l4rpe6$ju6$1...@newscl01ah.mathworks.com>...

Michelle Tadmor

unread,
Jan 30, 2014, 2:12:08 PM1/30/14
to
I used this solution
getChildren ---> set marker size for 1:3:end

Unfortunately from version 2013b you have to skip 3 only from 5 on (I guess they added some junk components in the beginning). (backward compatibility anyone?)

Can Matlab just address this issue?


"Elizabeth " <ea...@unh.edu> wrote in message <l4rqjm$76u$1...@newscl01ah.mathworks.com>...

Iain Strachan

unread,
Feb 17, 2016, 8:30:12 AM2/17/16
to
"Jimy Shah" wrote in message <ghq3qs$q36$1...@fred.mathworks.com>...
None of the methods that involve findobj on the legend object work as of 2015b (possibly earlier) as the legend object has been made somewhat opaque. There is a hidden property "EntryContainer", however, which by a tortuous route allows the marker size to be changed:

hL = legend( ... );
entries = hL.EntryContainer.Children;

for i=1:numel(entries)
entries(i).Icon.Transform.Children.Children(1).Size = <desired size>;
end

Note "Children.Children" in above is deliberate. The second Children object may contain 1 or 2 elements, the first of which is the marker, and the second of which is the line, unless the plot marker is just a line. You can test the class of the Children with isa(<obj>, 'matlab.graphics.primitive.world.LineStrip') ( or 'matlab.graphics.primitive.world.Marker').

There is, of course, absolutely no guarantee that this solution will work in future versions as they could well change the object structure.

Iain Strachan

unread,
Feb 17, 2016, 9:20:11 AM2/17/16
to
"Iain Strachan" wrote in message <na1skr$sm4$1...@newscl01ah.mathworks.com>...
Further to the above, there is a further caveat. Immediately after the call to "legend" the properties (Icon.Children.Children) don't seem to be present - it is necessary to do a drawnow() before starting to operate on the legend structure. Here's an example bit of code that seems to work:

function l = LegendWithBigMarkers(MarkerSize,varargin)
% function l = LegendWithBigMarkers(MarkerSize,varargin)
% Plots a legend with the varargin arguments and then
% changes any legends with markers to the new markers size

l = legend([varargin]);
drawnow;
entries = l.EntryContainer.Children;
for i = 1:numel(entries)
primitives = entries(i).Icon.Transform.Children.Children;
for j=1:numel(primitives)
disp(class(primitives(j)));
if(isa(primitives(j),'matlab.graphics.primitive.world.Marker'))
primitives(j).Size = MarkerSize;
end
end
end

-----
An alternative that doesn't delve into undocumented features is for each graph you want in the legend entry to plot an extra dummy graph of a single NaN so it's not visible, with the markersize set to the size required in the legend, and then plot the legend only for the dummy plots (using the "Entries" argument).

Iain Strachan

unread,
Feb 17, 2016, 9:51:12 AM2/17/16
to
"Iain Strachan" wrote in message <na1vij$5ih$1...@newscl01ah.mathworks.com>...
Small correction - call to "legend" in above should read:

l = legend(varargin{:});
0 new messages