matlab - matlab中图例标记的高级定制

标签 matlab customization legend

在matlab图例中对marker进行基本修改比较简单。以下代码片段 (basic legend) 生成的图例:

稍等 h = plot(inf,inf,'ob',inf,inf,'r+'); legend(h,'Data1','Data2');

可以轻松修改为:modified legend使用以下代码:

[~,~,icons,~] = legend(h,'Data1','Data2'); 设置(图标(1),'LineStyle','-') 设置(图标(2),'LineStyle','-')

但是,如果我想正确地为 object1 这样的对象添加图例,事情就会变得相当复杂。 (圆圈不在直线中间)或 object2 (一行带有“+”标记的几种颜色)。我没有找到任何允许修改图例框中标记位置或在一个图例组中添加多个标记的属性或解决方法。

有谁知道包含图例自定义高级信息的文档吗?或者如何更好地利用matlab提供的图形对象的众多属性来实现上面所描述的?

最佳答案

R2014a 之前的 MatLab 版本中,legend 框实际上是一个 axes,因此通过句柄修改其内容相对容易.

R2014b 版本开始,legend 是一个图形对象,似乎无法访问轴句柄(引用 this post on undocumentedmatlab ).

高达 R2014a

给定图中两条线的图例:

h = plot((0:.1:2*pi),sin((0:.1:2*pi)),'ob',(0:.1:2*pi),cos((0:.1:2*pi)),'r+');
[a,b,icons,c] = legend(h,'Data1','Data2');
  • a 是图例 axex 的句柄
  • b 是一个 handel 数组:
    • b(1): 第一个字符串的句柄
    • b(2): 第二个字符串句柄
    • b(3): 第一行句柄
    • b(4): 第一行标记句柄
    • b(5): 第二行句柄
    • b(6):第二行标记句柄

如果您想将第一行的标记移动到例如行尾,您可以:

  • 获取该行的XData(存储在b(3)中):它是一个(1x2)数组
  • markerXData(保存在b(4)中)设置为上一步得到的数组的最后一个值

如果你想添加更多的marker并且让第二行由更多不同颜色的段组成,你可以:

  • 获取该行的XDataYData(存储在b(5)中)
  • 通过拆分XData 数组生成x 坐标
  • 使用 YData 值作为 y 坐标for 循环中绘制线段

此方法已在以下代码中实现,其中图例框也已放大以使其更具“可读性”。

代码中的注释应该解释不同的步骤。

% Plot something
h = plot((0:.1:2*pi),sin((0:.1:2*pi)),'ob',(0:.1:2*pi),cos((0:.1:2*pi)),'r+');
% Add trhe legend
[a,b,icons,c] = legend(h,'Data1','Data2');
%
% a ==> handle of the legend axes
% b(1) ==> handle of the first string
% b(2) ==> handle of the second string
% b(3) ==> handle of the first line
% b(4) ==> handle of the marker of the first line
% b(5) ==> handle of the second line
% b(6) ==> handle of the marker of the second line
%
% Get positin and size of the legend box
ax_p=get(a,'position')
% Enlarge the legend box
set(a,'position',[ax_p(1)-.2 ax_p(2) ax_p(3)+.2 ax_p(4)])
% Set the linestyle of the first element on the legend
set(b(3),'linestyle','-')
% Get the XData of the first line
xl_1=get(b(3),'xdata')
% Move the marker of the first line to the end of the line
set(b(4),'xdata',xl_1(2))
% Get the position of the first string
xs_1=get(b(1),'position')
% Move the first string
set(b(1),'position',[xs_1(1)+.2 xs_1(2) xs_1(3)])
% Get the position of the second string
xs_2=get(b(2),'position')
% Move the second string
set(b(2),'position',[xs_2(1)+.2 xs_2(2) xs_2(3)])
% Split the second line in multi-color segment and add more marker on the
% second line
%
% Define the number of segments
n=4;
% Get the XData of the first line
xl_2=get(b(5),'xdata')
% Get the YData of the first line
yl_2=get(b(5),'ydata')
% Define the segments
len=linspace(xl_2(1),xl_2(2),n+1);
% Plot the segments of the second line in different colours
for i=1:n
   plot(a,[len(i) len(i+1)],[yl_2(1) yl_2(2)], ...
      'marker',get(b(6),'marker'),'markeredgecolor', ...
      get(b(6),'markeredgecolor'),'markerfacecolor',get(b(6),'markerfacecolor'), ...
      'color',rand(1,3),'linewidth',2)
end

这是结果:

enter image description here

来自 R2014b

由于似乎无法访问图例 axex,解决方案可能是(如上面提到的 post 中所建议的那样,添加一个 axes 并将其叠加到图例上。

您可以先创建图例:

h = plot((0:.1:2*pi),sin((0:.1:2*pi)),'o-',(0:.1:2*pi),cos((0:.1:2*pi)),'r+-');
[a,b,icons,c] = legend(h,'Data1','Data2');
  • a 是 matlab.graphics.illustration.Legend 的对象(尝试 class(a))
  • b 是 matlab.graphics.primitive.Data 对象的数组(尝试 class(b))

与旧版本类似,b 指的是:

  • b(1): 第一个字符串
  • b(2): 第二个字符串
  • b(3): 第一行
  • b(4): 第一行标记
  • b(5):第二行
  • b(6): 第二行标记

可以通过legend对象a<获取legendpositionsize/.

然后您可以应用上述相同的方法来绘制“更新的”图例。

此方法已在以下代码中实现(注释应解释不同的步骤)。

% Plot something
h = plot((0:.1:2*pi),sin((0:.1:2*pi)),'o-',(0:.1:2*pi),cos((0:.1:2*pi)),'r+-');
% Add the legend
[a,b,icons,c] = legend(h,'Data1','Data2');
% Add an axes to the figure
ax=axes;
% Enlarge the legend, then set the axes position and size equal to the
% legend box
%Get the legend's position and size
ax_p=a.Position;
a.Position=[ax_p(1)-.2 ax_p(2) ax_p(3)+.2 ax_p(4)];
ax.Position=a.Position;
ax.Units='normalized';
ax.Box='on';
% Plot the firt line in the axes
plot(ax,b(3).XData,b(3).YData,'color',b(3).Color);
hold on
% Add the marker of the first line at the end of the line
plot(ax,b(3).XData(end),b(3).YData(end), ...
             'marker',b(4).Marker, ...
             'markeredgecolor',b(4).Color, ...
             'markerfacecolor',b(3).MarkerFaceColor);
% Get second line XData and YData
x=b(5).XData;
y=b(5).YData;
% Define the number of line sections
n=5;
% Update the XData and YData by defning intermediate values
len=linspace(x(1),x(2),n);
% Plot the set of line with different colours
for i=1:n-1
   plot(ax,[len(i) len(i+1)],[y(2) y(2)], ...
      'marker',b(6).Marker,'markeredgecolor',b(6).Color, ...
      'markerfacecolor',b(6).MarkerFaceColor, ...
      'color',rand(1,3),'linewidth',1.5);
end
% Get the legend texts position
pt1=b(1).Position;
pt2=b(2).Position;
% Add the legend text
text(pt1(1)+.1,pt1(2),a.String{1});
text(pt2(1)+.1,pt2(2),a.String{2});
% Remove the axes ticks
ax.XTick=[];
ax.YTick=[];
% Set the axes limits
ax.XLim=[0 1];
ax.YLim=[0 1];

希望这对您有所帮助。

卡普拉'

关于matlab - matlab中图例标记的高级定制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36069297/

相关文章:

android - 自定义 SeekBar 的矩形边缘

python - 如何在 matplotlib 中更改图例字体名称

r - ggplot2 在此特定示例中更改图例标题

r - ggplot2:一个图例,具有从公共(public)变量派生的两个视觉属性

image-processing - 在 MATLAB 中处理图像的一部分

matlab - 使用简单矩阵乘法时出错

iphone - 自定义 UISearchDisplayController

r - 逆矩阵中的误差

pdf - "Publish"到 pdf

javascript - 自定义图像复选框