Matlab 中图例标记的高级定制

2023-11-24

It is relatively simple to add basic modifications to markers in matlab legends. The legend produced by the following code snippet (basic legend):

hold on h = plot(inf,inf,'ob',inf,inf,'r+'); legend(h,'Data1','Data2');

Can be easily modified to: modified legend using the following code:

[~,~,icons,~] = legend(h,'Data1','Data2'); set(icons(1),'LineStyle','-') set(icons(2),'LineStyle','-')

However things become fairly complicated if I want to correctly legend objects such as object1 (circle is not in the middle of a line) or object2 (several colors for one line with '+' markers on it). I have not found any property or workaround that allows to modify the position of the markers in the legend box, or add several markers in one legend group.

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


在 MatLab 版本中高达R2014a the legend盒子实际上是一个axes所以通过它的句柄修改它的内容是比较容易的。

从版本R2014b the legend is a graphics object似乎没有办法访问轴句柄(参考这篇关于未记录的 matlab 的文章).

直至 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 is an array of handels:
    • b(1):第一个字符串的句柄
    • b(2):第二个字符串的句柄
    • b(3):第一行的句柄
    • b(4):第一行标记的句柄
    • b(5):第二行句柄
    • b(6):第二行标记的句柄

如果你想移动marker例如,从第一行到行尾,您可以:

  • get the XData该行的(存储在b(3)):它是一个 (1x2) 数组
  • set the XData of the marker(存储在b(4)) 到上一步中获得的数组的最后一个值

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

  • get the XData and YData该行的(存储在b(5))
  • 生成x coord通过拆分XData array
  • 绘制a中的段for循环使用YData价值为y coord

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

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

% 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');
  • a 是一个对象matlab.graphics.illustration.Legend (try class(a))
  • b 是一个数组matlab.graphics.primitive.Data对象(尝试class(b))

与旧版本类似,b指:

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

您可以获得position and size of the legend通过legend object a.

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

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Matlab 中图例标记的高级定制 的相关文章

  • 垂直子图的单一颜色条

    我想让下面的 MATLAB 图有一个沿着两个子图延伸的颜色条 像这样的事情 使用图形编辑器手动完成 Note 这与提出的问题不同here https stackoverflow com questions 39950229 matlab t
  • 将数据提示堆栈放在轴标签顶部,并在轴位置发生更改后更新轴标签

    此问题仅适用于 unix matlab Windows 用户将无法重现该问题 我在尝试创建位于 y 轴标签顶部的数据提示时遇到问题 下图很能说明问题 正如您所看到的 在 ylabel 附近创建的数据提示将到达 ylabel 文本的底部 而期
  • Matlab颜色检测

    我试图一致地检测同一场景的图像之间的某种颜色 这个想法是根据颜色配置文件识别一组对象 因此 例如 如果给我一个带有绿色球的场景 并且我选择绿色作为我的调色板的一部分 我想要一个具有反映它检测到球的矩阵的函数 任何人都可以为这个项目推荐一些
  • 如何为已编译的 MATLAB 创建安装程序并要求用户接受我们的许可条款?

    我正在 MATLAB 中编写程序分发给 Windows 用户 我使用 MATLAB 编译器和 MATLAB r2014a 版本来创建程序 我可以使用 MATLAB 应用程序编译器创建 Windows 安装程序 并且它的工作效果可以接受 但是
  • 直方图均衡结果

    I am trying to code histogram equalization by my self but the results are different from the built in function in matlab
  • 如何在向量中的所有点之间绘制线?

    我有一个包含二维空间中一些点的向量 我希望 MATLAB 用从每个点到每个其他点绘制的线来绘制这些点 基本上 我想要一个所有顶点都连接的图 你能用情节来做到这一点吗 如果可以 怎么做 一种解决方案是使用该函数为每个点组合创建一组索引MESH
  • Matlab:条形图中缺少标签

    使用 Matlab 2012 和 2013 我发现设置XTickLabel on a bar图表最多只能使用 15 个柱 如果条形较多 则标签会丢失 如下所示 绘制 15 个条形图 N 15 x 1 N labels num2str x d
  • 以 2 为底的矩阵对数

    Logm 取矩阵对数 并且log2 取矩阵每个元素以 2 为底的对数 我正在尝试计算冯 诺依曼熵 它涉及以 2 为底的矩阵对数 我该怎么做呢 如果将 以 2 为底 的矩阵指数定义为B expm log 2 A 或者如果您类似地通过特征分解直
  • 将 Matlab 数组移植到 C/C++

    我正在将 matlab 程序移植到 C C 我有几个问题 但最重要的问题之一是 Matlab 将任何维度的数组都视为相同 假设我们有一个这样的函数 function result f A B C result A 2 B C A B and
  • 如何在 MATLAB 编译的应用程序中运行外部 .m 代码? [复制]

    这个问题在这里已经有答案了 我有一个 MATLAB 项目 我使用 MCC 对其进行编译以获得单个可执行文件 然后我想知道外部程序员是否可以在 exe 中执行他的一些 m 文件 而无需重新编译整个项目 重点是提供一个应用程序 其他开发人员可以
  • 在网格中制作一个矩形图例,并标记行和列

    我有一个 ggplot 我将因子映射到填充和 alpha 如下所示 set seed 47 the data lt data frame value rpois 6 lambda 20 cat1 rep c A B each 3 cat2
  • 如何在 MATLAB 中将矩阵元素除以列总和?

    有没有一种简单的方法可以将每个矩阵元素除以列和 例如 input 1 4 4 10 output 1 5 4 14 4 5 10 14 以下是执行此操作的不同方法的列表 使用bsxfun https www mathworks com he
  • matlab 中的动画绘图

    我正在尝试创建一个三角形的动画图 最终结果应该是十个三角形 后面跟着两个更大的三角形 后面跟着一条直线 使用matlab文档 https de mathworks com help matlab ref drawnow html 我最终得到
  • 自定义 Visual Studio 2008 中的位置栏

    有人成功定制了 VS 2008 的 Places Bar 吗 我从 VS 2005 进行的自定义设置并没有转移到 2008 显然 并且无论我如何处理注册表 我都无法使我的自定义位置出现在 打开 对话框中 我已经阅读并应用了相关的MS KB文
  • 更新:随机将行添加到矩阵中,但遵循严格的规则

    以下是一个更大的矩阵的一部分 0 1 0000 1 0000 77 0000 100 0000 0 0 2500 0 1 0000 1 0000 72 0000 100 0000 0 2500 0 2500 0 1 0000 1 0000
  • 通过颜色渐变修补圆

    我正在尝试绘制一个颜色渐变 我希望它沿轴均匀 在下图由角度定义的情况下 pi 7 当我使用patch命令 绘图与所需的梯度方向匹配 但沿其方向并不均匀 沿圆的点之间形成各种三角形 这是代码 N 120 theta linspace pi p
  • 我可以定义自定义字符类简写吗?

    Java 提供了一些有用的字符类 例如 d and w 我可以定义自己的角色类别吗 例如 能够为字符类定义简写 例如 A Za z 我可以定义自己的角色类别吗 不 你不能 就个人而言 当我有一个 稍微 复杂的正则表达式时 我将正则表达式分解
  • 如何从 matlab 调用 Qtproject?

    我在 matlab 中有一个函数可以写入一个 file txt 我在 qt 项目中使用它 So 当我使用 unix 获取要运行的 qt 编译可执行文件时 我有一个 Matlab 文件 但出现错误 代码 unix home matt Desk
  • 使用 R 将图例添加到绘图中

    我使用 R 在一个图中创建了 4 条线 这是代码 Define 2 vectors cars lt c 123 07 110 51 96 14 98 71 101 3 trucks lt c 110 31 89 91 89 81 89 31
  • 拟合具有扭曲时基的正弦波

    我想知道在 Matlab 中拟合具有扭曲时基的正弦波的最佳方法 时间失真由 n 阶多项式 n 10 给出 其形式为t distort P t 例如 考虑失真t distort 8 12t 6t 2 t 3 这只是幂级数展开 t 2 3 这将

随机推荐

  • django:根据条件排除某些表单元素

    我有一些表单字段 我想根据是否满足特定条件来包含 排除这些字段 我知道如何包含和排除表单元素 但是当我希望它的元素根据函数的结果显示时 我很难做到这一点 这是我的表格 class ProfileForm ModelForm this tea
  • AWS EC2 究竟如何计算每小时成本?

    简单的问题 如果我有六个相同的 EC2 实例处理数据正好十分钟然后关闭 我会被收取六个小时还是一小时的费用 Update EC2 和 EBS 现在基于使用情况下降到第二个 旧答案变更的粒度精确到小时 来自 AWS 定价网站http aws
  • Blazor Checkbox双向绑定和更改事件

    绑定复选框并在该复选框更改时触发事件的正确方法是什么 我尝试了几种不同的方法 但都没有完全按照我的希望工作 请注意 该复选框位于组件中
  • 如何将sender参数传递给system.timers.timer

    如何将sender参数传递给system timers timer在 NET 中 timer1 timer new System Timers Timer interval timer1 timer AutoReset true timer
  • 如何使用 R 从 MATLAB 序列日期数字中提取时间?

    我有一些需要在 R 中使用的 MATLAB 序列日期号 但我必须将它们转换为正常日期 Matlab datestr 733038 6 ans 27 Dec 2006 14 24 00 你可以看到它给出了日期和时间 Now we try in
  • 如何将字符串转换回列表

    我有一个清单 ab 1 2 a b c I did strab str ab So strab现在是一个字符串 我想将该字符串转换回列表中 我怎样才能做到这一点 最简单和最安全的方法是使用ast literal eval import as
  • Firebase:提供的存储桶与 Swift 中当前实例的存储桶不匹配

    我有以下代码 let storageRef FIRStorage reference forURL gs slugbug appspot com dots intentional let imageRef storageRef child
  • React useContext() 性能,自定义钩子内的 useContext

    我使用了一个结构反应钩子 它是基于全球Context包含减速器的组合 如 Redux 中 另外 我广泛使用定制挂钩来分离逻辑 我有一个包含异步 API 请求的钩子 它变得相当麻烦 我有机会将该钩子的几乎每个函数拆分为其他钩子 但每个函数都使
  • ADB 命令切换不会保留活动开发人员选项设置,但没有实际效果

    正如标题所说 如果我发出以下命令 adb shell settings put global always finish activities 1 如果我转到 开发人员选项 我可以看到切换从 关闭 更改为 打开 还 adb shell se
  • 列出 SQL Server 2005 中哪些列具有全文索引

    如何列出数据库中具有全文索引的所有表 列 select distinct object name fic object id table name name column name from sys fulltext index colum
  • 使用 Amazon RedShift 透视表

    我在 Amazon RedShift 中有多个表 它们遵循多个维度列和一对指标名称 值列的模式 DimensionA DimensionB MetricName MetricValue dimA1 dimB1 m1 v11 dimA1 di
  • 在 ruby​​ 中使用 sjcl.js 创建的 AES 解密

    您好 让我们假设客户端有一个密钥 该密钥不通过与加密数据相同的通道传输 我想要完成的是解密结果斯坦福 Javascript 加密库 sjcl 在红宝石中 或者对于具有支持 AES 的加密库的任何其他语言的概括 这是我在 javascript
  • ?#iefix 如何解决 IE6-IE8 中的网页字体加载问题?

    网上有很多这样的文章 http www fontspring com blog fixing ie9 font face problems建议添加一个 iefix到 eot 网址 我很想知道how这能解决问题吗 谢谢 IE8 及更早版本的
  • 405 尽管 CORS 仍不允许方法

    我正在尝试使用 Angular 开发前端应用程序 由于我添加了授权标头对于 HTTP POST 和 GET 请求 我得到405 不允许的方法 尽管我似乎允许服务器端的一切 我的 Chrome 浏览器中的调试器说它要求Access Contr
  • XGBoost 从增强器对象中获取分类器对象?

    我通常使用以下方法来表达特征重要性 regr XGBClassifier regr fit X y regr feature importances 其中 type regr 是 但是 我有一个腌制的 mXGBoost 模型 解包后会返回一
  • Nuxt + Vuetify。如何应用主题颜色

    我正在使用 Nuxt js Vuetify js 项目 查看文件assets style app styl we have Import and define Vuetify color theme https vuetifyjs com
  • 如何删除 Chart.js 中轴的线条/规则?

    我设法使用以下方法删除图表中的所有水平线 规则 scales xAxes gridLines display false 但我也想去掉代表 Y 轴的规则 条 但我想保留标签 不幸的是我找不到任何选择 我只能删除整个轴 包括标签 我正在使用
  • MongoDB - 错误:getMore 命令失败:找不到游标

    我需要创建一个新字段sid大约 500K 文档集合中的每个文档 每个sid是唯一的并且基于该记录的现有记录roundedDate and stream fields 我使用以下代码来执行此操作 var cursor db getCollec
  • ajax加载tab后的回调

    如何将一些代码应用于 ajax 加载选项卡的内容 我尝试在加载的内容中使用 document ready 但这阻止了 css 样式的加载 不知道为什么 有回调函数吗 我应该以其他方式在加载的文档中使用 document ready 和样式吗
  • Matlab 中图例标记的高级定制

    It is relatively simple to add basic modifications to markers in matlab legends The legend produced by the following cod