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

请先登录,再进行评论。

更多回答(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