How can the colors of ones choice be added to the legends automatically in an image ?
0 Comments
Accepted Answer
2 Comments
You can change the labels of the yaxis to anything youd like. You can also make a fake legend with patches but that would require a bit of work.
If you draw a sketch of what you want then I might be able to give you some code.
This question from earlier today seems relevant. Actually, it is probably exactly what you are looking for.
https://se.mathworks.com/matlabcentral/answers/421247-colorbar-for-specified-color
I adapted the code for you
sg=1:16;
for i=1:length(sg)
for j=1:20
x(i,j)=i;
end
end
imagesc(x)
cbp=colorbar('southoutside');
cbp.Visible='off';
p=cbp.Position;
w=p(3)/numel(sg)
cmap=colormap(jet(length(sg)))
str=sprintfc('\n Class %g',1:numel(sg))
for i=1:numel(sg)
annotation('textbox',[p(1)+w*(i-1) p(2) w*0.95 p(4)],'backgroundcolor',cmap(i,:),'string',str{i})
end

Also see my attached example for making the bar vertical. This is more complicated, as a break is automatically inserted when the text reaches the horizontal edges of the textbox. You will need to adapt the position properties to make it look good.
More Answers (1)

0 Comments
See Also
Categories
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!