Legend for a patches object

68 views (last 30 days)
Nolan
Nolan on 28 Jul 2015
Commented: Kelly Kearney on 28 Jul 2015
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
Star Strider
Star Strider on 28 Jul 2015
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)

Kelly Kearney
Kelly Kearney on 28 Jul 2015
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);

Nolan
Nolan on 28 Jul 2015
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
Kelly Kearney
Kelly Kearney on 28 Jul 2015
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.

Community Treasure Hunt

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

Start Hunting!