How to label legend of plot

6 views (last 30 days)
N/A
N/A on 1 Jul 2022
Commented: Voss on 1 Jul 2022
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;

Accepted Answer

Voss
Voss on 1 Jul 2022
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 Comments
N/A
N/A on 1 Jul 2022
Awesome. Works great! I appreciate your time greatly.
Kind Regards,
Matthew
Voss
Voss on 1 Jul 2022
You're welcome!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!