How to label legend of plot
3 views (last 30 days)
Show older comments
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;
0 Comments
Accepted Answer
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-')
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-')
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'})
More Answers (0)
See Also
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!