MATLAB Answers

How can I change the marker size in legend?

634 views (last 30 days)
Hwee
Hwee on 9 Oct 2012
Commented: Walter Roberson on 12 Nov 2019
Hi, I am having a trouble with chaning the marker size in legned. I plotted some graphs as follows.
plot(Model.Period,Model.QQ,'o','MarkerEdgeColor','k', 'LineWidth', 2, ...
'MarkerFaceColor','none','MarkerSize',3);
ylabel('Q(cms)','FontWeight','bold');
hold on;
area(Model.Period,Model.QQ, 'FaceColor',[.6 .9 .8],'EdgeColor','k');
hold off;
xlim([min(Model.Period) max(Model.Period)]);
Graph.leg3 = legend('Q as o-symbol','Q as area plot');
set(Graph.leg3,'Location','NorthEast', 'FontWeight', 'bold').
I want to increase the size of marker in the legend without changing the marker size in the graphs. Can you help me, please? Thank you.

  4 Comments

Show 1 older comment
RT5
RT5 on 19 Jul 2016
You still can change the marker size in the legend. You need to use the icons.Children.MarkerSize property of legend. Here is a link to a nice explanation:
Just tried this out in R2015b and works fine.
Lauren Cook
Lauren Cook on 7 Mar 2018
I just tried doing this with MATLAB 2014b with the following code
sz = 20:30:(20+30*5);
[h,icons,plots,legend_text] = legend('1hour','3hour','6hour','12hour','24hour','Location','NorthWest');
for i = 1:length(icons)
icons(i).Children.MarkerSize = sz(i);
end
|And I got the following error: |
No public field MarkerSize exists for class matlab.graphics.GraphicsPlaceholder.
Error in plot_depth_qqplot_allDRTM (line 54)
icons(i).Children.MarkerSize = sz(i);
I also tried this:
for i = 1:length(icons)
set(icons(i),'MarkerSize', sz(i));
end
And got this error:
Error using matlab.graphics.primitive.Text/set
There is no MarkerSize property on the Text class.
I am trying to set each marker size in the legend equal to the size in the figure. The size increases as the duration for that point increase (e.g., 1hr to 24 hrs). Is is actually possible to change the individual marker size in a legend? If so, what am I doing wrong? Thanks for any help.
Walter Roberson
Walter Roberson on 23 Sep 2019
sz = 20:30:(20+30*5);
That is 6 entries
[h,icons,plots,legend_text] = legend('1hour','3hour','6hour','12hour','24hour','Location','NorthWest');
That has 5 legend entries. The mismatch is likely leading to difficulties.

Sign in to comment.

Answers (10)

Shawn Daugherty
Shawn Daugherty on 10 Sep 2018
Edited: Shawn Daugherty on 10 Sep 2018
This might be a roundabout way but in R2017b this works for me:
% Create a legend with 3 entries
[h,icons] = legend('Entry 1','Entry 2','Entry 3');
% Find the 'line' objects
icons = findobj(icons,'Type','line');
% Find lines that use a marker
icons = findobj(icons,'Marker','none','-xor');
% Resize the marker in the legend
set(icons,'MarkerSize',20);
Hope this helps.

  1 Comment

Walter Roberson
Walter Roberson on 11 Sep 2018
I am not sure why your findobj() ends in -xor ? You would normally need another condition after that.
Perhaps,
icons = findobj(icons, '-not', 'Marker', 'none');
In a more general context where icons might be mixed objects,
icons = findobj(icons, '-property', 'Marker', '-and', '-not', 'Marker', 'none');

Sign in to comment.


Sachin Ganjare
Sachin Ganjare on 9 Oct 2012

  9 Comments

Show 6 older comments
Hwee
Hwee on 9 Oct 2012
How can I turn on 'ShowHiddenHandles' property?
Sachin Ganjare
Sachin Ganjare on 9 Oct 2012
plot_handle = plot(Model.Period,Model.QQ,'o','MarkerEdgeColor'.....
set(plot_handle, 'ShowHiddenHandles', 'on')

Sign in to comment.


Lionel Trébuchon
Lionel Trébuchon on 11 May 2016
I am interested too! Neither "children" nor "findobj" seem to work in Matlab 2015b!
Friendly greetings, Lionel

  0 Comments

Sign in to comment.


Jeffers
Jeffers on 11 Aug 2016
Edited: Walter Roberson on 11 Aug 2016
A brute force (not very elegant) kind of thing I often do (Matlab 2015b, but should work in all) is to create a new plot of a single point (on the same figure) with the same marker type and the size you require in the legend. Just make sure the point doesnt show up in your graph (e.g. by controlling the axis limits using axis([xmin xmax ymin ymax])).
E.g.
plot(4:1:8,1:2:9,'x','MarkerSize',10,'Color',[1 0 0]);
hold on;
axis([0 9 0 9]);
p=plot(100,100,'x','MarkerSize',50,'Color',[1 0 0]);
l=legend(p,'Big Marker','Location','NorthWest');
set(l,'Interpreter', 'latex','FontSize',20);
Of course, you can do this with several different types of data point (marker), where 'p' becomes a vector e.g. p(1)=plot();p(2)=plot();...

  1 Comment

Walter Roberson
Walter Roberson on 11 Aug 2016
You can use nan or inf for the point coordinates to ensure it will not render the points without having to adjust the plotting limits.

Sign in to comment.


Ramalingam Kailasham
Ramalingam Kailasham on 9 Mar 2018
legendmarkeradjust does not give a satisfactory performance with R2017b. The legend markers are of a different style from the figure. LaTeX interpretation also seems to be a problem. Shouldn't there be a simpler way to adjust the legend marker size??!

  0 Comments

Sign in to comment.


Azzi Abdelmalek
Azzi Abdelmalek on 9 Oct 2012
h=legend(text')
set(h,'fontsize',20)

  6 Comments

Show 3 older comments
Hwee
Hwee on 9 Oct 2012
I am trying to change the marker size in the legend.
Azzi Abdelmalek
Azzi Abdelmalek on 9 Oct 2012
t=0:0.1:10
y=sin(t)
plot(t,y)
ax2=axes
plot(0,0,'linewidth',10,'parent',ax2)
legend(ax2,'txt')
set(ax2,'visible','off')

Sign in to comment.


Andreas
Andreas on 1 Apr 2014
Edited: Andreas on 4 Apr 2014

  3 Comments

Matthew Cooper
Matthew Cooper on 7 Jul 2014
Thanks Andreas that is a great function.
Luis Pazos
Luis Pazos on 3 Dec 2015
Hi Andreas,
Do you know if this script still works? I have tried it in the last version of matlab, and doesn't seem to be able to find any children for legend.
I paste the response of the first four lines of you code in my figure:
Input:
s=get(legend)
s1=s.Children
s2=[]
s2=findobj(s1,{'type','patch','-or','type','line'})
Output:
s =
Box: 'on'
Color: [1 1 1]
EdgeColor: [1 1 1]
FontName: 'Cambria'
FontSize: 32
FontAngle: 'normal'
FontWeight: 'normal'
Interpreter: 'tex'
LineWidth: 0.6000
Location: 'northeast'
Orientation: 'vertical'
Position: [0.5988 0.6667 0.2943 0.1901]
String: {'Sqrt(PL) = n(x)' 'Photocurrent'}
TextColor: [0 0 0]
Units: 'normalized'
Children: []
Parent: [1x1 Figure]
Visible: 'on'
HandleVisibility: 'on'
UIContextMenu: [1x1 ContextMenu]
BusyAction: 'queue'
BeingDeleted: 'off'
Interruptible: 'off'
CreateFcn: ''
DeleteFcn: ''
ButtonDownFcn: @bdowncb
Type: 'legend'
Tag: 'legend'
UserData: []
Selected: 'off'
SelectionHighlight: 'on'
HitTest: 'on'
PickableParts: 'visible'
s1 =
0x0 empty GraphicsPlaceholder array.
s2 =
[]
s2 =
0x0 empty GraphicsPlaceholder array.
I also attach the figure in which I'm applying this. What I would need is basically to make the lines of the legend larger, because at the moment they are disproportionately small compared to the letters. I would really appreciate help with this.
Thanks in advance, Luis
Walter Roberson
Walter Roberson on 11 May 2016
As of R2014b, legend no longer have children.

Sign in to comment.


Ioannis Matthaiou
Ioannis Matthaiou on 9 Mar 2016
hi, has anyone found a solution as to how to adjust markersize property of the legend in matlab 2015b? thanks, yiannis

  0 Comments

Sign in to comment.


Andreas
Andreas on 24 Mar 2017
The function adjust legend marker size has now been updated to work with Matlab 2016b http://www.mathworks.com/matlabcentral/fileexchange/46105-adjust-legend-marker-size

  0 Comments

Sign in to comment.


Jing Ci Neo
Jing Ci Neo on 23 Sep 2019
Edited: Jing Ci Neo on 23 Sep 2019
I find that this works. For example if I want to change the symbol size of the second and third symbols:
[hLg, icons]=legend('Lines','A','B','C');
icons = findobj(icons,'Type','patch');
icons = findobj(icons,'Marker','none','-xor');
set(icons(2:3),'MarkerSize',11);

  4 Comments

Show 1 older comment
Jing Ci Neo
Jing Ci Neo on 23 Sep 2019
I must admit I have no idea and I'm curious about it as well. Both "-or" and "-xor" works, and both "-and" and "-not" does not work
Hannes Schwingenschlögl
Hannes Schwingenschlögl on 12 Nov 2019
is there a possibility of getting the variable "icons" out of an existing legend without creating a new one?
Walter Roberson
Walter Roberson on 12 Nov 2019
No. When the icons output is requested, legend builds the internal structure differently. The properties do not exist in a way that can be changed when icons is not used.

Sign in to comment.

Sign in to answer this question.