MATLAB: How can the colors of ones choice be added to the legends automatically in an image

legends

I have an image with 16 classes. I wish to add legends (Class1, Class2,…,Class16) to this image corresponding to the color generated by imagesc colormap. The sample image can be displayed using the code below:
for i=1:16
for j=1:20
x(i,j)=i;
end
end
imagesc(x)

Best Answer

usually when you have a 3d plot like that you use the colorbar instead of a legend. Try adding this:
colormap(parula(16))
colorbar
it will give you a colorbar with 16 distinct colors.