how to merge two legend in one?
Mostra commenti meno recenti
Hello every one,
i have three plots and i want to have just one legend for plot p1 and p2 ,for example for the following code:
x=0:0.25:2*pi;
y=sin(x);
p1=plot(x,y);
hold on
p2=plot(x,y,'o','MarkerSize',12,'MarkerFaceColor','b','MarkerEdgeColor','b');
p3=plot(x+0.02,y+0.02,'o','MarkerSize',4,'MarkerFaceColor','w','MarkerEdgeColor','w');
as you see i have merged two markers(a big blue circle and a small white circle ) and i want two show these two circles together as same legend.is there any way to do it?

3 Commenti
That's a nice effect...never had thought of doing...
Minor side note:
Can do with only two line handle objects instead of three--
hL(1)=plot(x,y,'o-','MarkerSize',12,'MarkerFaceColor','b','MarkerEdgeColor','b');
hold on
hL(2)=plot(x+0.02,y+0.02,'o','MarkerSize',4,'MarkerFaceColor','w','MarkerEdgeColor','w');
Arash Hajisharifi
il 8 Mar 2020
dpb
il 8 Mar 2020
I tried
hL(2)=plot(x+dxy,y+dxy,'or','MarkerSize',4);
also and thought kinda' cute...
Risposta accettata
Più risposte (3)
Nathan Miller
il 19 Ott 2021
Modificato: Walter Roberson
il 13 Mag 2023
If you dig in deep enough you absolutely can customize the legend icons to whatever you want programatically, but it's quite messy and 'undocumented'.
I came up with my solution below after looking through Yair's https://undocumentedmatlab.com/blog_old/plot-legend-customization and thinking outside the box a bit with a colleague. The key was doing the copy of the node object to create a new Marker primitive and then updating the Parent property that loads it into the legend handle to be drawn.
% Plot OP's code
figure; hold all; % new fig, enable hold/add
x=0:0.25:2*pi;
y=sin(x);
hL(1)=plot(x,y,'o-','MarkerSize',12,'MarkerFaceColor','b','MarkerEdgeColor','b');
hL(2)=plot(x+0.02,y+0.02,'o','MarkerSize',4,'MarkerFaceColor','w','MarkerEdgeColor','w');
% Add legend for the first/main plot handle
hLegend = legend(hL(1),'location','best');
drawnow(); % have to render the internal nodes before accessing them
% Extract legend nodes/primitives
hLegendEntry = hLegend.EntryContainer.NodeChildren(1); % first/bottom row of legend
iconSet = hLegendEntry.Icon.Transform.Children.Children; % array of first/bottom row's icons (marker+line)
% Create a new icon marker to add to the icon set
newLegendIcon = copy(iconSet(1)); % copy the object (or look into making a matlab.graphics.primitive.world.Marker)
newLegendIcon.get % list all properties
newLegendIcon.Parent = iconSet(1).Parent; % set the parent, adding to the legend's icon draw set
% Mess with the new icon's properties to show how you want
newLegendIcon.FaceColorData = uint8([255;255;255;255]); % rgba uint8
newLegendIcon.VertexData(1) = 0.53; % [0-1] within the icon's boundaries (not the +0.02)
newLegendIcon.VertexData(2) = 0.65; % [0-1] within the icon's boundaries (not the +0.02)
newLegendIcon.Size = 4; % a little different from MarkerSize it seems
Output image if the auto-run figure from the above code doesn't generate correctly:

Bonus extra figure/example showing how you can separate the icon markers to show multiple of them on the same line:

4 Commenti
Zhang Jianyu
il 13 Mag 2023
Can you show me how you plot the legend of the last graph?
Here, something to this effect:
% Plot OP's code
figure; hold all; % new fig, enable hold/add
x=1:5;
y=x;
hL(1)=plot(x,y,'ob');
hL(2)=plot(x,y+1,'r-s');
% Add legend for the first/main plot handle
hLegend = legend(hL,'location','best');
drawnow(); % have to render the internal nodes before accessing them
%%% Update top row legened (blue circle)
% Extract legend nodes/primitives
hLegendEntryTop = hLegend.EntryContainer.NodeChildren(end); % top row of legend
iconSet = hLegendEntryTop.Icon.Transform.Children.Children; % array of first/bottom row's icons (marker+line)
% Move primary marker over to the edge
iconSet(1).VertexData(1) = 0.90;
% Create a new icon marker to add to the icon set
newLegendIcon1 = copy(iconSet(1)); % copy the object (or look into making a matlab.graphics.primitive.world.Marker)
newLegendIcon1.Parent = iconSet(1).Parent; % set the parent, adding to the legend's icon draw set
newLegendIcon1.Style = 'plus';
newLegendIcon1.VertexData(1) = 0.10;
% Create a new icon marker to add to the icon set
newLegendIcon2 = copy(iconSet(1)); % copy the object (or look into making a matlab.graphics.primitive.world.Marker)
newLegendIcon2.Parent = iconSet(1).Parent; % set the parent, adding to the legend's icon draw set
newLegendIcon2.Style = 'asterisk';
newLegendIcon2.VertexData(1) = 0.50;
%%% Update bottom row legend (red square + line)
% Extract legend nodes/primitives
hLegendEntryBtm = hLegend.EntryContainer.NodeChildren(1); % bottom row of legend
iconSet = hLegendEntryBtm.Icon.Transform.Children.Children; % array of first/bottom row's icons (marker+line)
% Move primary marker over
iconSet(1).VertexData(1) = 0.25;
% Create a new icon marker to add to the icon set
newLegendIcon = copy(iconSet(1)); % copy the object (or look into making a matlab.graphics.primitive.world.Marker)
newLegendIcon.Parent = iconSet(1).Parent; % set the parent, adding to the legend's icon draw set
newLegendIcon.Style = 'pentagram';
newLegendIcon.VertexData(1) = 0.75;
newLegendIcon.Size = 8;
Jakub
il 25 Mar 2025
I have used your solution and it worked well until I wanted to save the plot as a picture. Regardless of the picture format (.eps or .png...), Matlab has overridden the legend formating back to the standard one while saving the plot.
I have no clue why.
Walter Roberson
il 25 Mar 2025
It depends on which method you used to save the plot as a picture.
Functions such as print() and exportgraphics() build internal copies of figures -- copies with the proper page size and centering implemented. The process of building internal copies of figures involves rerunning the legend() command, which leads to the legend being re-rendered with its default properties.
This problem should not occur if you use getframe ... but that might not be high-enough resolution for your purposes.
Hakan Caldag
il 28 Mar 2023
Modificato: Hakan Caldag
il 7 Nov 2023
For anyone who is happy to keep the legend somewhere outside the axes, I ended up with another solution that looks simpler to me: Copying the axes object to have a new one right on top, having two legends for each and setting the top legend color to 'none' so it becomes transparent and both icons become visible:
x=0:0.25:2*pi;
y=sin(x);
subplot(121);
p1=plot(x,y);
hold on
p2=plot(x,y,'o','MarkerSize',12,'MarkerFaceColor','b','MarkerEdgeColor','b');
p3=plot(x+0.02,y+0.02,'o','MarkerSize',4,'MarkerFaceColor','w','MarkerEdgeColor','w');
legend('','Anything here','Position',[0.6 0.6 0.1 0.1])
axhand=gca; %axes handle
duplicateplot=copyobj(axhand,1); % the duplicate
axes(duplicateplot);
% Removing the additional labels/ticks of the second axes object
xticks([]);yticks([]);xlabel('');ylabel('');
legend('','',' ','Position',[0.52 0.6036 0.135 0.1],'Color','none','EdgeColor','none')
% Setting the colors to none is what makes the icons overlap
% Adjust the position any way you would like here, I tried to match the
% shape in the plot.
% Notice that the ' ' label is set for the plot object with the white icons
Jonas
il 18 Giu 2025
0 voti
Hi,
I've managed to use this methodology to edit some legend entries (using Matlab 2024a), but I'm facing some problems.
For one, when I export to pdf using the function in the window, under File->Export setup (which I do because it generally produces better results for my figures), the first/bottom entry, a line, is overwritten. I have edited the entry for a patch object, which does not get overwritten. I've tried editing other entries after editing the line, making sure that the line objects are deleted, but it still happens. Not sure what's going on here.
I also have error bars (without a line) in the figure. This method seems completely incompatible with error bars, as the 'iconSet' object is an empty graphics placeholder. I've been looking around seemingly everywhere top-down from the legend object itself, but I can't find the actual icon that's being rendered.
Any idea how to deal with these issues?
Categorie
Scopri di più su Legend in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

