Matlab中图例标记的高级定制化

5
在Matlab的图例中添加基本修改相对简单。以下代码片段(basic legend)生成的图例: hold on h = plot(inf,inf,'ob',inf,inf,'r+'); legend(h,'Data1','Data2'); 可以很容易地通过以下代码修改为:modified legend: [~,~,icons,~] = legend(h,'Data1','Data2'); set(icons(1),'LineStyle','-') set(icons(2),'LineStyle','-') 但如果我想正确地为诸如object1(圆圈不在线中间)或object2(一条线有多种颜色,上面带有“+”标记)这样的对象做图例,事情就变得相当复杂了。我还没有找到任何属性或解决办法,可以修改图例框中标记的位置或在一个图例组中添加多个标记。
有没有人知道有包含图例自定义高级信息的文档?或者如何更好地使用Matlab提供的众多图形对象属性来实现上述描述?
1个回答

4
在MatLab版本中,直到R2014a,图例框实际上是一个轴(axes),因此通过其句柄相对容易地修改其内容。
从版本R2014b开始,图例是一个图形对象(graphics object),似乎没有办法访问轴的句柄(参考这个帖子: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是图例轴的句柄
  • b是一个句柄数组:
    • b(1): 第一个字符串的句柄
    • b(2): 第二个字符串的句柄
    • b(3): 第一条线的句柄
    • b(4): 第一条线的标记句柄
    • b(5): 第二条线的句柄
    • b(6): 第二条线的标记句柄

如果您想将第一条线上的标记移动到该行的末尾,可以:

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

如果您想添加更多的标记,并且使第二条线由不同颜色的多个线段组成,可以:

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

这种方法已经在以下代码中实现,图例框也已被放大,使其更易读。

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

% 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 开始

由于似乎无法访问图例轴,因此可以采用以下建议(在上述 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');
  • amatlab.graphics.illustration.Legend对象(尝试使用class(a)
  • bmatlab.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];

希望这有所帮助。

Qapla'


Qapla',非常感谢您抽出时间撰写如此详尽而清晰的答案。感谢您提供的一般信息,也感谢您提供的更个性化的建议。 真希望我能给您一百个赞! - 0000011111
不用谢!很高兴能对你有所帮助!这是一个非常有趣的问题。 - il_raffa
对于R2014b+语法,文档中声明了[lgd,icons,plots,txt] = legend(...)。实际名称并不重要,但移动输出参数icons的位置有点令人困惑。第三个输出参数(plots)是图轴中与图例相关的图形对象的句柄。正如你在这里所说的,第二个输出(icons)是用于绘制图例本身的句柄。 - Cris Luengo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接