How can I change the marker size in legend?

822 views (last 30 days)
Hwee
Hwee on 9 Oct 2012
Edited: Adam Danz on 29 Sep 2020
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
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 (11)

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.
  2 Comments
Adam Danz
Adam Danz on 18 Apr 2020
Note that this solution uses undocumented outputs to legend() that have caused problems in recent releases of Matlab (since r2018b). For a full explanation of this problem, see this comment.

Sign in to comment.


Adam Danz
Adam Danz on 18 Apr 2020
Edited: Adam Danz on 29 Sep 2020
How to create legend components that are customizable
Copy the plotted objects and replace their (x,y,z) coordinates with NaN values so the copied objects do not appear in the plot. You can change the graphics properties to the copied objects and use their handles to create the customizable legend.
Demo
% Create the plot
ax = axes();
hold on
h(1) = plot(linspace(1,5,25), rand(1,25), 'ro', 'DisplayName', 'foo');
h(2) = plot(1:5, rand(1,5), 'b-', 'DisplayName', 'bar');
% copy the objects
hCopy = copyobj(h, ax);
% replace coordinates with NaN
% Either all XData or all YData or both should be NaN.
set(hCopy(1),'XData', NaN', 'YData', NaN)
set(hCopy(2),'XData', NaN', 'YData', NaN)
% Note, these lines can be combined: set(hCopy,'XData', NaN', 'YData', NaN)
% To avoid "Data lengths must match" warning, assuming hCopy is a handle array,
% use arrayfun(@(h)set(h,'XData',nan(size(h.XData))),hCopy)
% Alter the graphics properties
hCopy(1).MarkerSize = 15;
hCopy(1).LineWidth = 2;
hCopy(2).LineWidth = 3;
% Create legend using copied objects
legend(hCopy)
Note that some graphics objects have different property names for XData and YData.
Why you souldn't use solutions that rely on legend() outputs
Since r2018b only the first output of the legend function is documented. Archived documentation for the legend function from r2016a to r2018a suggests avoiding the use of the outputs other than the first output:
Note: This syntax is not recommended and creates a legend that does not support all graphics features. Use the l = legend(__) syntax to return the legend object and set Legend Properties instead.
For example, if the first and second outputs to legend() are both included, it interferes with the ability to change the fontsize of the legend text.

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??!

Sachin Ganjare
Sachin Ganjare on 9 Oct 2012
  9 Comments

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

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.


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);
  6 Comments
Adam Danz
Adam Danz on 18 Apr 2020
As Walter pointed out, this solution uses udocumented outputs to legend() that have caused problems in recent releases of Matlab (since r2018b). For a full explanation of this problem, see this comment.

Sign in to comment.


Azzi Abdelmalek
Azzi Abdelmalek on 9 Oct 2012
h=legend(text')
set(h,'fontsize',20)
  6 Comments
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
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

Community Treasure Hunt

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

Start Hunting!