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 Center および File ExchangeLegend についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by