如何制作两个标记在MATLAB图中共享相同的标签

发布于 2025-01-19 06:05:53 字数 455 浏览 3 评论 0原文

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

  1. 如何创建两个图例对象对于单个绘图实例?
  2. 两个共享相同的标签图例中带有线和点标记的图

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

谢谢

I am building a MATLAB script to add two markers for a single label similar to below in Python.

  1. How to create two legend objects for a single plot instance? and
  2. Sharing the same label for two plots with line and point markers in legend

Does anyone know a way to modify the legend in MATLAB to include two markers adjacent to a common label?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

提笔书几行 2025-01-26 06:05:53

稍微修改帖子此处< /a>,你可以得到你想要的。请注意,这使用了未记录的功能,但它在 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

输入图片此处描述

Slightly modifying the post here, you can get what you want. Note that this uses undocumented features, but it still works in 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

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文