How can I change the marker size in legend?

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

Ken
Ken 2016년 5월 31일
You used to be able to do this. The kiddies keep changing things for the worse. Mathworks: Leave things alone if the alternative is reduced capability.
RT5
RT5 2016년 7월 19일
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.
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.
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.

댓글을 달려면 로그인하십시오.

답변 (11개)

Shawn Daugherty
Shawn Daugherty 2018년 9월 10일
편집: Shawn Daugherty 2018년 9월 10일

8 개 추천

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

Walter Roberson
Walter Roberson 2018년 9월 11일
편집: Walter Roberson 2018년 9월 11일
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');
Adam Danz
Adam Danz 2020년 4월 18일
편집: Adam Danz 2020년 4월 18일
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.

댓글을 달려면 로그인하십시오.

Adam Danz
Adam Danz 2020년 4월 18일
편집: Adam Danz 2020년 9월 29일

7 개 추천

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.

댓글 수: 6

Sim
Sim 2022년 8월 30일
편집: Sim 2022년 8월 30일
@Adam Danz if I use a "scatter" instead of a "plot"
h(2) = scatter(1:5, rand(1,5), 10, 'DisplayName', 'bar');
It does not recognise the MarkerSize
hCopy(1).MarkerSize = 15;
How to make the trick you proposed, but with the "scatter" instead of "plot" ?
Sim
Sim 2022년 8월 30일
편집: Sim 2022년 8월 30일
@Adam Danz I tried to use "SizeData" instead of "MarkerSize", but it does not work:
for i = 1 : 2
hCopy(i) = copyobj(h1(i), ax);
set(hCopy(i),'XData', NaN', 'YData', NaN)
% hCopy(i).SizeDataMode = 'manual';
hCopy(i).SizeData = 15;
end
@Sim, you must be using scatter objects. Since a single scatter object can have varying marker sizes, the size of the marker in the legend does not represent a particular marker size.
Instead of copying scatter objects, just use plot to create the legend symbol.
Example:
h = scatter(6:-1:1,1:6,50:50:300,'bo');
ax = gca();
hold on
% Create legend marker
hCopy = plot(nan,nan,'bo','MarkerSize',20,'DisplayName', 'foo');
legend(hCopy)
Sim
Sim 2022년 9월 17일
Super!!! Thanks a lot @Adam Danz !! :-)
Sim
Sim 2023년 6월 1일
편집: Sim 2023년 6월 1일
@Adam Danz Me again, Hi! ...What if I want slightly transparent symbols in the legend. Indeed, I can set transparency for scatter:
scatter(x,y,'o','MarkerEdgeAlpha',0.2)
but I cannot do the same for plot...right?
Any workaround to add transparency to the legends symbols, and still using your method?
Adam Danz
Adam Danz 2023년 6월 1일
Transparency for line objects is not fully supported and does not have a property similar to MarkerEdgeAlpha. Instead of copying the axes objects and duplicating them for the legend, you could replace the line object with a scatter object that has the same color and size but applies MarkerEdgeAlpha.

댓글을 달려면 로그인하십시오.

Jeffers
Jeffers 2016년 8월 11일
편집: Walter Roberson 2016년 8월 11일

3 개 추천

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

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.

댓글을 달려면 로그인하십시오.

Ramalingam Kailasham
Ramalingam Kailasham 2018년 3월 9일

3 개 추천

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??!
Jing Ci Neo
Jing Ci Neo 2019년 9월 23일
편집: Jing Ci Neo 2019년 9월 23일

3 개 추천

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);

댓글 수: 7

I am confused about the purpose of the -xor that is not followed by a condition?
Jing Ci Neo
Jing Ci Neo 2019년 9월 23일
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
is there a possibility of getting the variable "icons" out of an existing legend without creating a new one?
Walter Roberson
Walter Roberson 2019년 11월 12일
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.
Samuel Nathan
Samuel Nathan 2020년 4월 16일
This Code worked perfectly in 2019b.
Adam Danz
Adam Danz 2020년 4월 18일
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.
Ritam
Ritam 2025년 10월 8일
This worked perfect on MATLAB R2022b. Thank you so much :)

댓글을 달려면 로그인하십시오.

Sachin Ganjare
Sachin Ganjare 2012년 10월 9일

1 개 추천

댓글 수: 9

Hwee
Hwee 2012년 10월 9일
I added:
a=get(Graph.leg3,'children'); set(a(1),'MarkerSize',20);
based on your suggestion. But still it does not works. Can any one give me the method to solve this problem?
Thank you.
Sachin Ganjare
Sachin Ganjare 2012년 10월 9일
Did you tried the solution mentioned at the end of thread I shared?
Hwee
Hwee 2012년 10월 9일
Yes, I tried but it does not work.
Sachin Ganjare
Sachin Ganjare 2012년 10월 9일
Probably below link helps:
It shows usage of 'Position' property for legend resizing
Hwee
Hwee 2012년 10월 9일
I only need to how to change the marker size in legend not fontsize and the box size of the legend. Thank you for your help but it does not solve my problem.
Sachin Ganjare
Sachin Ganjare 2012년 10월 9일
편집: Sachin Ganjare 2012년 10월 9일
I think your 'ShowHiddenHandles' property is 'OFF'. Probabaly switching it to ON & then applying solution in thread will help.
Hwee
Hwee 2012년 10월 9일
How can I turn on 'ShowHiddenHandles' property?
Sachin Ganjare
Sachin Ganjare 2012년 10월 9일
plot_handle = plot(Model.Period,Model.QQ,'o','MarkerEdgeColor'.....
set(plot_handle, 'ShowHiddenHandles', 'on')
Hwee
Hwee 2012년 10월 9일
it does not work

댓글을 달려면 로그인하십시오.

Lionel Trébuchon
Lionel Trébuchon 2016년 5월 11일

1 개 추천

I am interested too! Neither "children" nor "findobj" seem to work in Matlab 2015b!
Friendly greetings, Lionel
Andreas
Andreas 2017년 3월 24일

1 개 추천

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
Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 9일

0 개 추천

h=legend(text')
set(h,'fontsize',20)

댓글 수: 6

Hwee
Hwee 2012년 10월 9일
편집: Hwee 2012년 10월 9일
this changes only the size of text in legend.
Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 9일
편집: Azzi Abdelmalek 2012년 10월 9일
set(h,'fontsize',20,'Position', [.1,.2,.1,.2])
adjust your position
Hwee
Hwee 2012년 10월 9일
I think you do not understand my point.
can you explain?
Hwee
Hwee 2012년 10월 9일
I am trying to change the marker size in the legend.
Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 9일
편집: Azzi Abdelmalek 2012년 10월 9일
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')

댓글을 달려면 로그인하십시오.

Andreas
Andreas 2014년 4월 1일
편집: Andreas 2014년 4월 4일

0 개 추천

댓글 수: 3

Thanks Andreas that is a great function.
Luis Pazos
Luis Pazos 2015년 12월 3일
편집: Walter Roberson 2019년 9월 23일
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
As of R2014b, legend no longer have children.

댓글을 달려면 로그인하십시오.

Ioannis Matthaiou
Ioannis Matthaiou 2016년 3월 9일

0 개 추천

hi, has anyone found a solution as to how to adjust markersize property of the legend in matlab 2015b? thanks, yiannis

카테고리

질문:

2012년 10월 9일

댓글:

2025년 10월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by