如何修改Matlab绘图中的图例?

如何修改Matlab绘图中的图例?,matlab,plot,legend,Matlab,Plot,Legend,我想画四个两种颜色的圆圈。我在用圆函数画一个圆。我正面临legend()的问题。它使用相同的颜色为两个数据着色 function main clear all clc circle([ 10, 0], 3, 'b') circle([-10, 0], 3, 'b') circle([ 10, 10], 3, 'r') circle([-10, 10], 3, 'r') % Nested function to draw a circle function circle(c

我想画四个两种颜色的圆圈。我在用圆函数画一个圆。我正面临
legend()
的问题。它使用相同的颜色为两个数据着色

function main
clear all
clc

circle([ 10,  0], 3, 'b')
circle([-10,  0], 3, 'b')
circle([ 10, 10], 3, 'r')
circle([-10, 10], 3, 'r')

    % Nested function to draw a circle
    function circle(center,radius, color)
     axis([-20, 20, -20 20])
     hold on;
     angle = 0:0.1:2*pi;
     
     grid on

     x = center(1) + radius*cos(angle);
     y = center(2) + radius*sin(angle);
     plot(x,y, color, 'LineWidth', 2);
     xlabel('x-axis');
     ylabel('y-axis');
     title('Est vs Tr')
     legend('true','estimated');
    end


end
下图显示了该问题。两种颜色都是蓝色,而其中一种是红色


有什么建议吗?

问题是你画了4个东西,在图例中只有2个条目。 因此,它将选择前四个事物的颜色来为图例着色

现在我没有机会亲自尝试,但我想最简单的“解决方案”是先画第三个圆,然后再画第二个圆

circle([ 10,  0], 3, 'b')
circle([ 10, 10], 3, 'r')
circle([-10,  0], 3, 'b')
circle([-10, 10], 3, 'r')

问题是,你画了4个东西,在图例中只有2个条目。 因此,它将选择前四个事物的颜色来为图例着色

现在我没有机会亲自尝试,但我想最简单的“解决方案”是先画第三个圆,然后再画第二个圆

circle([ 10,  0], 3, 'b')
circle([ 10, 10], 3, 'r')
circle([-10,  0], 3, 'b')
circle([-10, 10], 3, 'r')

您可以使函数
circle()
返回打印句柄。将句柄存储在向量中。最后,在绘制所有圆后,只需调用一次
legend()
。图例中的第一个参数是要显示在图例中的函数句柄。大概是这样的:

function main
% clear all % functions have their own workspace, this should always be empty anyway
clc
handles = NaN(1,2);
handles(1,1) = circle([ 10,  0], 3, 'b'); % handle of a blue circle
circle([-10,  0], 3, 'b')
handles(1,2) = circle([ 10, 10], 3, 'r'); % handle of a red circle
circle([-10, 10], 3, 'r')

    % Nested function to draw a circle
    function h = circle(center,radius, color) % now returns plot handle
     axis([-20, 20, -20 20])
     hold on;
     angle = 0:0.1:2*pi;
     grid on
     x = center(1) + radius*cos(angle);
     y = center(2) + radius*sin(angle);
     h = plot(x,y, color, 'LineWidth', 2);
     xlabel('x-axis');
     ylabel('y-axis');
     title('Est vs Tr')
    end

% legend outside of the function
legend(handles, 'true','estimated'); % legend for a blue and a red circle handle
end

结果如下所示:

您可以使函数
circle()
返回绘图句柄。将句柄存储在向量中。最后,在绘制所有圆后,只需调用一次
legend()
。图例中的第一个参数是要显示在图例中的函数句柄。大概是这样的:

function main
% clear all % functions have their own workspace, this should always be empty anyway
clc
handles = NaN(1,2);
handles(1,1) = circle([ 10,  0], 3, 'b'); % handle of a blue circle
circle([-10,  0], 3, 'b')
handles(1,2) = circle([ 10, 10], 3, 'r'); % handle of a red circle
circle([-10, 10], 3, 'r')

    % Nested function to draw a circle
    function h = circle(center,radius, color) % now returns plot handle
     axis([-20, 20, -20 20])
     hold on;
     angle = 0:0.1:2*pi;
     grid on
     x = center(1) + radius*cos(angle);
     y = center(2) + radius*sin(angle);
     h = plot(x,y, color, 'LineWidth', 2);
     xlabel('x-axis');
     ylabel('y-axis');
     title('Est vs Tr')
    end

% legend outside of the function
legend(handles, 'true','estimated'); % legend for a blue and a red circle handle
end

结果如下:

谢谢@Dennis。它解决了这个问题。但是我有更多的圆圈,所以我需要把它们作为一组颜色。这个问题还有别的解决办法吗?或者有没有其他方法不使用图例来绘制此框,以便我可以根据自己的意愿对其进行修改?@CroCo这有点模糊,无法回答,但如果您只想设置图例的颜色,或许您可以查看一下:谢谢@Dennis。它解决了这个问题。但是我有更多的圆圈,所以我需要把它们作为一组颜色。这个问题还有别的解决办法吗?或者有没有其他方法不使用图例来绘制此框,以便我可以根据自己的意愿对其进行修改?@CroCo这有点模糊,无法回答,但如果您只想设置图例的颜色,或许您可以查看一下:谢谢。这是一个很好的解决方案。谢谢。这是一个很好的解决方案。
function main
% clear all % functions have their own workspace, this should always be empty anyway
clc
handles = NaN(1,2);
handles(1,1) = circle([ 10,  0], 3, 'b'); % handle of a blue circle
circle([-10,  0], 3, 'b')
handles(1,2) = circle([ 10, 10], 3, 'r'); % handle of a red circle
circle([-10, 10], 3, 'r')

    % Nested function to draw a circle
    function h = circle(center,radius, color) % now returns plot handle
     axis([-20, 20, -20 20])
     hold on;
     angle = 0:0.1:2*pi;
     grid on
     x = center(1) + radius*cos(angle);
     y = center(2) + radius*sin(angle);
     h = plot(x,y, color, 'LineWidth', 2);
     xlabel('x-axis');
     ylabel('y-axis');
     title('Est vs Tr')
    end

% legend outside of the function
legend(handles, 'true','estimated'); % legend for a blue and a red circle handle
end