繁体   English   中英

MATLAB:如何将自定义图例添加到任何图

[英]MATLAB: how to add custom legend to any plot

如何在任何情节中添加自定义图例?
例如:

figure(200)
plot(t1:k,Pexact(t1:k,1),'-xk');
plot(t1:k,xh(1,t1:k),'-sr');

然后我想使用相同的代码添加图

hold on plot(t1:k,xh(1,t1:k),'-sb');

然后我想为所有三个情节添加图例

if exist('hl','var')
clear hl
end
hold on
hl(1) = plot(t1,0,'-sr');
hl(2) = plot(t1,0,'-sb');
hl(3) = plot(t1,0,'-xk');
set(hl,'LineWidth',2);
set(hl,'Visible','off');
legend(hl,'SC-PF','PF','Truth',...
    'Location','NorthWest');
  • 注意:将0,0更改为原始图中存在的ax,y坐标。

我更喜欢在绘制情节时定义图例。 如果特定集合中的数据丢失,或者您有条件集合,因此并非每次都绘制所有数据,则此方法更有意义。

figure(fig)
hold on
plot(t1:k,Pexact(t1:k,1),'-xk','DisplayName','SC-PF');
plot(t1:k,xh(1,t1:k),'-sr','DisplayName','PF');
plot(t1:k,xh(1,t1:k),'-sb','DisplayName','Truth')
legend(get(fig, 'Child'),'show')
legend('location','northwest')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM