I'm a matlab newbie and plotting an image as shown below. I'm facing difficulties while adding a legend to image(); in my code. I could use legend when I use plot(); but could someone help me to use legend with image(); please.
% Display Data_1 and Data_2
DisplayData = zeros(100,100,length(Set));
DisplayData(Data_1_Index) = Data_1;
DisplayData(Data_2_Index) = Data_2;
DisplayData(Data_1_Index) = 20; % Use light blue for Data_1
DisplayData(Data_2_Index) = 50; % Use yellow for Data_2
figure;
image(DisplayData(:,:));
title('Data_1 and Data_2 elements');
Help command for legend was not that helpful.

 채택된 답변

Image Analyst
Image Analyst 2018년 8월 16일

2 개 추천

What exactly would you want to see as a legend for an image? If I want text up there explaining something I use either title() or text(). I think those should do what you want. Lines and markers, like you get with a line plot don't really apply for an image.

댓글 수: 4

Shannon Cherry
Shannon Cherry 2018년 8월 16일
I have an image as shown below and I need a legend to indicate green color as 'Data_1' and yellow color as 'Data_2'
Image Analyst
Image Analyst 2018년 8월 16일
Yes, text() can do that. Try something like
axis('on', 'image');
% Display title without underlines causing subscripts
title('Data1 and Data2 elements', 'Interpreter', 'None', 'FontSize', 20, 'FontWeight', 'bold');
% Put up "legend"
str = sprintf('Data_1');
text(1, 260, str, 'Color', 'g', 'FontSize', 15, 'FontWeight', 'Bold', 'Interpreter', 'None');
str = sprintf('Data_2');
text(1, 220, str, 'Color', 'y', 'FontSize', 15, 'FontWeight', 'Bold', 'Interpreter', 'None');
This works after removing the line
axis('on', 'image');
and I moved the text from position 1 to 12. However, I couldn't understand what axis(); was doing there. Below is the output with axis(); uncommented.
Output after commenting out axis(); looks good.
Image Analyst
Image Analyst 2018년 8월 16일
axis('image') just makes it so that your image pixels are square. I thought you had used the XData option to scale your x axis, but I guess you didn't - your image is really only 14 pixels wide and 300 pixels tall so that's why it looks tall and narrow. If you don't use axis('image') then it will stretch your image in the x direction to give an image of a stretched, but more normal looking, aspect ratio.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Nathan Jessurun
Nathan Jessurun 2019년 10월 31일
편집: Nathan Jessurun 2019년 10월 31일

3 개 추천

To me, this looks better. I store the colors in my image and make empty scatterplots on top of the image:
% Try it out
bwImg = imfill(imread('coins.png') > 100, 'holes');
% bwImg will have several binary circles
[eachCoin, numCoins] = bwlabel(bwImg);
% Get a different color for each coin. Add one for background and skip it
allColors = parula(numCoins + 1);
allColors = allColors(2:end,:);
labelsArr = cell(1, numCoins);
for ii = 1:length(labelsArr)
labelsArr{ii} = ['coin' num2str(ii)];
end
imagesc(eachCoin);
imlegend(allColors, labelsArr);
function imlegend(colorArr, labelsArr)
% For instance if two legend entries are needed:
% colorArr =
% Nx3 array of doubles. Each entry should be an RGB percentage value between 0 and 1
%
% labelsArr =
% 1×N cell array
% {'First name here'} {'Second name here'} {'etc'}
hold on;
for ii = 1:length(labelsArr)
% Make a new legend entry for each label. 'color' contains a 0->255 RGB triplet
scatter([],[],1, colorArr(ii,:), 'filled', 'DisplayName', labelsArr{ii});
end
hold off;
legend();
end

댓글 수: 1

Ralph
Ralph 2020년 4월 6일
This is the best approach demonstrated. The task is not a legend for an 'image' per se, but rather for a 'map' with designated 2D regions.

댓글을 달려면 로그인하십시오.

Haritha
Haritha 2018년 8월 16일

0 개 추천

try this,
if true
legend('image 1','image 2')
end

카테고리

질문:

2018년 8월 16일

댓글:

2020년 4월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by