# How can the colors of ones choice be added to the legends manually in an image ?

1 view (last 30 days)
Shivam Pande on 28 Feb 2018
Answered: Walter Roberson on 1 Mar 2018
I wish to add the colors to the legends as per the rgb values of the corresponding color in the image. The image can be displayed using the code written below:
x1=ones(30); x2=2*ones(30); x3=3*ones(30);
x=[x1,x2,x3];
y1=zeros(30,90); y2=zeros(30,90); y3=zeros(30,90); y=zeros(30,90,3);
y1(x==1)=0; y2(x==1)=77; y3(x==1)=26;
y1(x==2)=102; y2(x==2)=255; y3(x==2)=102;
y1(x==3)=204; y2(x==3)=102; y3(x==3)=0;
y(:,:,1)=y1; y(:,:,2)=y2; y(:,:,3)=y3;
imshow(uint8(y))

Walter Roberson on 1 Mar 2018
You only have one graphics object (the image() object created by imshow), so you can only have one legend entry. The icon for an image is a very small image.
What you should probably do instead is to
lh(1) = line(nan, nan, 'Color', [0, 77, 26]/255);
lh(2) = line(nan, nan, 'Color', [102, 255, 102]/255);
lh(3) = line(nan, nan, 'Color', [204, 102, 0]/255);
legend(lh, {'Case 1', 'Case2', 'Case 3'})