Legend for multiple plots
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.
<</matlabcentral/answers/uploaded_files/56370/legend.png>>
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?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.
<</matlabcentral/answers/uploaded_files/56370/legend.png>>
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? 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.
<</matlabcentral/answers/uploaded_files/56370/legend.png>>
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? plot, plotting, legend MATLAB Answers — New Questions