如何使两个标记在 Matlab 图中共享相同的标签

How to make two markers share same label in Matlab plots

我正在构建一个 MATLAB 脚本来为单个标签添加两个标记,类似于下面 Python。

有谁知道在 MATLAB 中修改图例以包含两个与公共标签相邻的标记的方法?

谢谢

稍微修改一下posthere,就可以得到你想要的。请注意,这使用了未记录的功能,但它在 R2021a 中仍然有效。

figure; hold all; % new fig, enable hold/add
x=0:0.25:2*pi;
y=sin(x);
hL(1)=plot(x,y,'x','MarkerSize',10);
hL(2)=plot(x,y,'-','color',[1 0 0]);

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

% move the first icon to the left
LegendIcon1 = iconSet(1); 
LegendIcon1.VertexData(1) = 0.2;

% Create a new icon marker to add to the icon set
LegendIcon2 = copy(iconSet(1)); % copy the object (or look into making a matlab.graphics.primitive.world.Marker)
LegendIcon2.get % list all properties
LegendIcon2.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
LegendIcon2.Style = 'hbar';
LegendIcon2.FaceColorData = uint8([255;0;0;0]); % rgba uint8
LegendIcon2.EdgeColorData = uint8([255;0;0;0]); % rgba uint8

LegendIcon2.VertexData(1) = 0.6; % [0-1] within the icon's boundaries (not the +0.02)
% newLegendIcon.VertexData(2) = 0.5; % [0-1] within the icon's boundaries (not the +0.02)
LegendIcon2.Size = 10; % a little different from MarkerSize it seems