Legend for a patches object

From the Patches help page, I have made a graphic showing the FOV for a set of sensors. I have used the 'unique color' row in the face-coloring table. I included a screen grab of the section of the document. My question is, now that I have coloring for each face, I need to add a legend entry for each color. However, the patch is one graph object, or it seems like it. That is, I can only display 1 legend entry because there is only 1 patch. How can I populate the legend with all of the items I want.

1 Comment

We don’t have your graphic or your code. This makes it extremely difficult to provide you any sort of definitive help.

Sign in to comment.

Answers (2)

I usually create placeholder single-color patches to use for labeling purposes:
% A multi-faceted patch, one color per face
x = [2 5; 2 5; 8 8];
y = [4 0; 8 2; 4 0];
c = [0 1];
h = patch(x,y,c);
% Placeholder patches
unqc = unique(c);
label = cellstr(num2str(unqc'));
hold on;
for il = 1:length(unqc)
hl(il) = patch(NaN, NaN, unqc(il));
end
% Legend
legend(hl,label);
Here is my plotting code:
xData = [zeros(1,NUM_PROD);
classify_distances;
classify_distances];
yData = [sensorH*ones(1,NUM_PROD);
upperLim;
lowerLim];
% dummy data
xData = [0 0 0; 21 7 12; 21 7 12];
yData = [1 1 1; 6 4 1; -4 -1 .5];
figure;
p = patch(xData,yData,'r');
xlabel('Distance (m)'),ylabel('Height (m)');
% make different colors, make transparent
clear cdata
set(gca,'CLim',[0 40])
cdata = [15 30 25 2 60]';
set(p,'FaceColor','flat',...
'FaceAlpha',.2,...
'FaceVertexCData',cdata,...
'CDataMapping','scaled')
the reason Kelly's answer doesn't work is I don't know what the colormapping results were, other wise I could just make a dummy data just like she said and corresponding legend entry.
thoughts?

1 Comment

Your example doesn't quite run correctly... mismatched sizes in you x/yData vs cdata. Are you trying to assign one color per face? If so, the range of the colormap is irrelevant, as long as you set up your dummy patches using the same mapping.

Sign in to comment.

Categories

Asked:

on 28 Jul 2015

Commented:

on 28 Jul 2015

Community Treasure Hunt

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

Start Hunting!