如何在for循环后在图例中插入图例

问题描述 投票:0回答:1

我在MATLAB的3D图中插入图例时遇到问题。我有一个数据列表,尤其是我有一个nx3矩阵,上面填充有要绘制的数据,我想通过应用阈值来分离此数据。

在我的情况下,阈值是一个时间,因此,如果第i个数据低于阈值时间,它将以蓝色绘制,否则以红色绘制。

代码是

figure(1)
plot3(ra(1),dec(1),Time2plot(1),'*','Color','r', 'DisplayName', 'observation day');
hold on;
plot3(ra(end),dec(end),Time2plot(end),'*','Color','b','DisplayName', 'next day');
legend show;

for i = 1:length(Time2plot)
    if timeofday(Time2plot(i)) > B(1) && timeofday(Time2plot(i)) < B(2)
        hold on;
        plot3(ra(i),dec(i),Time2plot(i),'*','Color', 'b');

    else
        hold on;
        plot3(ra(i),dec(i),Time2plot(i),'*','Color','r');

    end
end

hold on;
title(['RA Dec in 3D ', date(1,1)]);
xlabel('RA');
ylabel('Dec');
zlabel('Time');
ztickformat('HH:mm:ss');
grid on;

其中B是阈值。结果为enter image description here

我只想在图例中显示两行:“第二天”和“观察日”,而不是所有数据。

matlab plot matlab-figure
1个回答
0
投票

如果您的图形已经有图例,并且再次使用legend命令,则旧的图例将被覆盖。因此,根据您的情况,您应该可以执行以下操作:

legend('observation day','next day');

只需将此行添加到您的代码中,您将得到两次输入的结果。当您要保留的图例条目不是前两个时,情况变得有些棘手。在这种情况下,您首先必须对轴对象的Children进行置换,以便它们成为第一个条目。假设'observation day'是图例的第3个条目,'next day'是第4个条目。在这种情况下,您需要在应用新图例之前执行以下操作:

h=gca;
h.Children=h.Children([(3:4).';(1:2).';(5:length(h.Children)).'])
© www.soinside.com 2019 - 2024. All rights reserved.