How to group individual plotted lines into one legend item

8 visualizzazioni (ultimi 30 giorni)
I have the following contour plot from this code. I'd like to group some of the lines I've made into one legend item. Alternatively, how can I plot in one line of code 3 line segments? I know there's an efficient way to do what I'm doing here...
Any help is appreciated.
f = @(x1, x2) 5*(x1).^2 + 7*(x2).^2 - 5*(x1) - 10*(x1).*(x2) + (x2) ;
%paths taken by each method in array for easy reference
%SD,FR,DFP,FBGS,N
SD = [5,2;3.8375,2.9765;2.3910,1.2546;2.0951,1.5032];
FR = [3.8375,2.9765;1.5,1];
DFP = [3.8375,2.9765;1.5185,0.9869;1.5320,1.0050;1.5414,1.0117];
BFGS = [3.8375,2.9765;2.4373,1.2832;2.1198,1.5279;1.7444,1.0738];
P = [5,2;2.1382,1.6729;1.5,1];
N = [5,2;1.5,1];
fcontour(f,[-2 6 -1 4],'LevelList',[-3 0 5 10 20 30 50 100]);
hold on
plot(1.5,1,'o') %optimal point
plot([SD(1,1),SD(2,1)],[SD(1,2),SD(2,2)]) %group 1
plot([SD(2,1),SD(3,1)],[SD(2,2),SD(3,2)]) %group 1
plot([SD(3,1),SD(4,1)],[SD(3,2),SD(4,2)]) %group 1
plot([FR(1,1),FR(2,1)],[FR(1,2),FR(2,2)]) %group 2
plot([DFP(1,1),DFP(2,1)],[DFP(1,2),DFP(2,2)]) %group 3
plot([DFP(2,1),DFP(3,1)],[DFP(2,2),DFP(3,2)]) %group 3
plot([DFP(3,1),DFP(4,1)],[DFP(3,2),DFP(4,2)]) %group 3
plot([BFGS(1,1),BFGS(2,1)],[BFGS(1,2),BFGS(2,2)]) %group 4
plot([BFGS(2,1),BFGS(3,1)],[BFGS(2,2),BFGS(3,2)]) %group 4
plot([BFGS(3,1),BFGS(4,1)],[BFGS(3,2),BFGS(4,2)]) %group 4
plot([P(1,1),P(2,1)],[P(1,2),P(2,2)]) %gruop 5
plot([P(2,1),P(3,1)],[P(2,2),P(3,2)]) %group 5
plot([N(1,1),N(2,1)],[N(1,2),N(2,2)]) %group 5
legend('F(x1,x2)','optimal','SD','FR','DFP','BFGS','P','N')
title('Contour Plot of F(x1,x2)')
xlabel('x1')
ylabel('x2')

Risposta accettata

Walter Roberson
Walter Roberson il 20 Feb 2020
You cannot do what you want to do.
You have two choicesb
1. record the handles returned by the graphic operations. Pass one member from each group as the first parameter to legend. This approach can work well in many cases.
2. Sometimes you have groups you want to legend that do not correspond exactly to graph objects. For example you might want to legend each different color that you generate with a single scatter(), or you want to legend by line pattern ignoring line color. In such a case you can not bother to record the handles of any of those objects. Instead, generate new objects with the desired legend appearance but coordinates nan or inf, and record those handles and pass them to legend as the first parameter. Using nan or inf forces them to not be drawn in the main plot, but legend will still create entries
  1 Commento
Sylvain Cornelus
Sylvain Cornelus il 20 Feb 2020
I could not understand how to assign handles to lines after reading the docs on MATLAB help. Thanks anyhow!
I did a work around where I just plotted lines from 0,0 to 0,0 in the right order such that when I 'labeled' them in my legend they correspond to the lines I want labeled. See below:
%% record values by method
SD = [5,2;3.8375,2.9765;2.3910,1.2546;2.0951,1.5032];
FR = [3.8375,2.9765;1.5,1];
DFP = [3.8375,2.9765;1.5185,0.9869;1.5320,1.0050;1.5414,1.0117];
BFGS = [3.8375,2.9765;2.4373,1.2832;2.1198,1.5279;1.7444,1.0738];
P = [5,2;2.1382,1.6729;1.5,1];
N = [5,2;1.5,1];
%% plot contour
x = linspace(-2,6);
y = linspace(-1,4);
[X,Y] = meshgrid(x,y);
Z = 5.*X.^2+7*Y.^2-5.*X-10.*X.*Y+Y;
v=[-3 0 5 10 20 30 50 100];
contour(X,Y,Z,v,'ShowText','on');
hold on
hold on
plot(1.5,1,'ok','MarkerSize',8) %optimal
plot(5,2,'*k','MarkerSize',8) %starting
%set up colors for legend as lines hidden behind the legend
plot([0,0],[0,0],'y')%steepest descent in yellow
plot([0,0],[0,0],'--r')%fletcher reeves in red
plot([0,0],[0,0],'g')%DFP in red
plot([0,0],[0,0],'--b')%BFGS in blue
plot([0,0],[0,0],'m')%Powell in magenta
plot([0,0],[0,0],'k')%BFGS in black
plot([SD(1,1),SD(2,1)],[SD(1,2),SD(2,2)],'y')
plot([SD(2,1),SD(3,1)],[SD(2,2),SD(3,2)],'y')
plot([SD(3,1),SD(4,1)],[SD(3,2),SD(4,2)],'y')
plot([FR(1,1),FR(2,1)],[FR(1,2),FR(2,2)],'--r')
plot([DFP(1,1),DFP(2,1)],[DFP(1,2),DFP(2,2)],'g')
plot([DFP(2,1),DFP(3,1)],[DFP(2,2),DFP(3,2)],'g')
plot([DFP(3,1),DFP(4,1)],[DFP(3,2),DFP(4,2)],'g')
plot([BFGS(1,1),BFGS(2,1)],[BFGS(1,2),BFGS(2,2)],'--b')
plot([BFGS(2,1),BFGS(3,1)],[BFGS(2,2),BFGS(3,2)],'--b')
plot([BFGS(3,1),BFGS(4,1)],[BFGS(3,2),BFGS(4,2)],'--b')
plot([P(1,1),P(2,1)],[P(1,2),P(2,2)],'m')
plot([P(2,1),P(3,1)],[P(2,2),P(3,2)],'m')
plot([N(1,1),N(2,1)],[N(1,2),N(2,2)],'k')
legend('F(x1,x2)','True Optimal','Starting','Steepest Descent','Fletcher Reeves','DFP','BFGS','Powell','Newton','Location','EastOutside')
title('Contour Plot of F(x1,x2) and Optimization Paths of Each Method')
xlabel('x1')
ylabel('x2')

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by