I want to generate a legend for multiple plots on the same figure using 'hold on;'. I am using a certain method I found through googling but I am not getting expected results. Here is my code.
sf = 1; % scaling factor
lambda_b = 10; % BS process density
lambda_c = 10; % cluster process density c <= b
lambda_u = 50; % user process density; large enough to make each BS active
% Base station process
npointsb = poissrnd(lambda_b*sf); % poisson variable; no. of points
pprocb = rand(npointsb,2); % uniform dist of points
xb = pprocb(:,1);
yb = pprocb(:,2);
% Cluster process
npointsc = poissrnd(lambda_c*sf);
pprocc = rand(npointsc,2);
xc = pprocc(:,1);
yc = pprocc(:,2);
% User process
npointsu = poissrnd(lambda_u*sf);
pprocu = rand(npointsu,2);
xu = pprocu(:,1);
yu = pprocu(:,2);
figure();
[vxb,vyb] = voronoi(xb,yb);
[vxc,vyc] = voronoi(xc,yc);
p1 = plot(vxb,vyb,'b','userdata','BS tessellation'); hold on;
p2 = plot(xb,yb,'rs','userdata','BS');
p3 = plot(vxc,vyc,'r','userdata','Cluster tessellation');
p4 = plot(xu,yu,'go','userdata','User'); hold off;
legend(get(gca, 'children'), get(get(gca, 'children'), 'userdata'));
axis([0 1 0 1]);
The result of this code is illustrated in the image.
You can see there are multiple entries and I suspect that is because the variables returned by the voronoi function are matrices and not vectors like the other variables. It seems this is making the plot function return a column vector of Line objects and this is why I am getting the multiple legend entries.
How do I get around this and plot the correct legend ignoring the multiple entries?

1 件のコメント

Vijayanand Sivakumar
Vijayanand Sivakumar 2021 年 3 月 28 日
Thank you very much for the solution.

サインインしてコメントする。

 採用された回答

Jonathan Mayers
Jonathan Mayers 2016 年 7 月 20 日

27 投票

Hi all,
I have obtained the desired legend output using handles. See the code below. The code generating the points did not change.
figure();
[vxb,vyb] = voronoi(xb,yb);
[vxc,vyc] = voronoi(xc,yc);
p1 = plot(vxb,vyb,'b'); hold on;
p2 = plot(xb,yb,'rs');
p3 = plot(vxc,vyc,'r');
p4 = plot(xu,yu,'go'); hold off;
axis([0 1 0 1]);
% Create column vector of plot handles
% Variables returned from voronoi are matrices and plotting them
% returns a column vector of handles so extract the first one
h = [p1(1);p2;p3(1);p4];
% Now call the legend function passing the handle h and specify the text
legend(h,'BS tessellation','BS','Cluster tessellation','User');
The below image verifies correct output of the code.

7 件のコメント

Héctor Carrasco López
Héctor Carrasco López 2018 年 12 月 5 日
You are my hero.
Miguel Marques
Miguel Marques 2019 年 5 月 23 日
移動済み: Voss 2024 年 5 月 23 日
You are my hero too.
Shivam Chopra
Shivam Chopra 2020 年 6 月 1 日
Hi, what should I do if I need to insert legends for these plots plotted in loop??
a = 1;
p = [1];
figure();
for B = [1E-9 1E-8 1E-7 1E-6 1E-5 1E-4 1E-3]
abs_z = diffusion(B);
f = (0:10:10^5);
p(a) = plot(log10(f),log10(abs_z));
a = a+1;
hold on;
end
hold off;
function abs_z = diffusion(B)
t_n = 1E-6;
d_n = 1E+6;
q = 1.6E-19;
m_n = 9.11E-31;
f = (0:10:10^5);
w_c = (q * B)/(m_n);
w = f.*(2*pi);
d_real = 1 + ((t_n^2) * ((w.^2) + (w_c^2))) ;
d_img = w.*((t_n)*((t_n.^2) * (((w_c.^2) - w.^2)-1))) ;
d_divider = (1 + ((t_n.^2) * (((w_c.^2) - w.^2).^2)) + ((w.*2*(t_n)).^2)) ;
z = complex(d_real,d_img);
d_m = d_n./d_divider;
abs_z = abs(z).*d_m;
end
Thanks in advance
Walter Roberson
Walter Roberson 2020 年 6 月 2 日
p(a) = plot(log10(f), log10(abs_z), 'DisplayName', sprintf('B = %.0e', B));
and at the end of the loop.
legend show
Tingmingke Lu
Tingmingke Lu 2020 年 6 月 30 日
This comes in handy! Thanks!
anup mallick
anup mallick 2023 年 6 月 1 日
Thanks...After a long search here and there, I got your answer, it worked for me..
Lau Lauridsen
Lau Lauridsen 2024 年 5 月 23 日
Thanks. Finnaly a useful solution

サインインしてコメントする。

その他の回答 (1 件)

Taniel Winner
Taniel Winner 2021 年 7 月 24 日

1 投票

I have found this code INCREDIBLY HELPFUL!!! It works!! Download it!

カテゴリ

質問済み:

2016 年 7 月 20 日

移動済み:

2024 年 5 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by