How to label legend of plot

조회 수: 2 (최근 30일)
N/A
N/A 2022년 7월 1일
댓글: Voss 2022년 7월 1일
Below is (part of) a code that generates two ellipses on one plot.
My question is: how can I use the legend function to label each ellipse as "MTE1" and "source report". Note, the two plot functions listed each plot one ellipse on the same plot.
%% Plot MTE1:
[a_squared,b_squared,OA] = matrix_to_ellipse_squared(q11,q12,q22);
a = sqrt(a_squared)*2.5;
b = sqrt(b_squared)*2.5;
% Calculate X&Y parameters with MTE1 as center:
[X, Y, x_mtf, y_mtf] = calculate_location_and_errorXY(LAT_MTE1,LON_MTE1,EL_MTE1,...
LAT_MTE1,LON_MTE1,EL_MTE1,a,b,OA);
% Plot
plot(X,Y,'k*', x_mtf, y_mtf, 'k-')
hold on
%% Plot Source Report
[a_squared,b_squared,OA] = matrix_to_ellipse_squared(p11,p12,p22);
a = sqrt(a_squared)*2.5;
b = sqrt(b_squared)*2.5;
[X, Y, x_source, y_source] = calculate_location_and_errorXY(LAT_SR,LON_SR,EL_SR,...
LAT_MTE1,LON_MTE1,EL_MTE1,a,b,OA);
plot(X,Y,'b*', x_source, y_source, 'b-')
hold on
%% This following part really really helps if you want to zoom, but otherwise comment
axis([-2500 1500 -2500 1500])
legend() %%% how to place inputs in legend
xlabel('feet')
ylabel('feet')
plotErrorEllipses = 1;

채택된 답변

Voss
Voss 2022년 7월 1일
Something like this should work (using random data and using the second line from each plot call in the legend - adjust as necessary):
% random data
X = rand(1,10);
Y = rand(1,10);
x_mtf = rand(1,10);
y_mtf = rand(1,10);
% Get the line handles returned from plot()
h_mtf = plot(X,Y,'k*', x_mtf, y_mtf, 'k-')
h_mtf =
2×1 Line array: Line Line
hold on
% random data
X = rand(1,10);
Y = rand(1,10);
x_source = rand(1,10);
y_source = rand(1,10);
% Get the line handles returned from plot()
h_source = plot(X,Y,'b*', x_source, y_source, 'b-')
h_source =
2×1 Line array: Line Line
hold on
% give legend() only the lines you want to use in the legend,
% along with their names:
legend([h_mtf(2) h_source(2)],{'MTE1' 'source report'})
  댓글 수: 2
N/A
N/A 2022년 7월 1일
Awesome. Works great! I appreciate your time greatly.
Kind Regards,
Matthew
Voss
Voss 2022년 7월 1일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by