colorbar for specified color

Hi:
I want to add a colorbar in Matlab like below:
is there anyway to do this in Matlab?
Thanks!
Yu

 Accepted Answer

jonas
jonas on 28 Sep 2018
Edited: jonas on 28 Sep 2018
Based on the data and code given below in the comments, you can build a custom colorbar with textboxes. The result is quite nice!
The below code shows the steps involved, however it cannot be executed without adding it to the code which is provided by OP in the comments.
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

8 Comments

Yu, if you need more help, then attach your original indexed or gray scale image.
Yu Li
Yu Li on 28 Sep 2018
Edited: Yu Li on 28 Sep 2018
Jonas:
thanks for you suggestion, I'm able to generate the single color contour plot using colormap(jet(n)), but I met problems with the patch function.
I plan to
(1)generate the colorbar, and make it 'invisible' (2) patch the color at the location of colorbar.
below is my code:
cbp=colorbar('southoutside');
cbp.Visible='off';
ax_p=cbp.Position;
delta_sg=0.05;
x_sg_tmp=[ax_p(1),ax_p(1)+delta_sg,ax_p(1)+delta_sg,ax_p(1)];
y_sg_tmp=[ax_p(2),ax_p(2),ax_p(2)+delta_sg,ax_p(2)+delta_sg];
patch(x_sg_tmp,y_sg_tmp,'red')
I assume that I have only one color, to simply this problem. the parameter: x_sg_tmp and y_sg_tmp is to determine a rectangular whose bottom-left point is the same with colorbar, and having a size of delta_sg=0.05;
after I run this code, I could not see any patch result.
is there any mistake with my operation?
below is what I have with contour plot and colorbar.
Thanks!
Yu
Image Analyst
Image Analyst on 28 Sep 2018
Edited: Image Analyst on 28 Sep 2018
Did you see my comment? How can we help you if you won't attach your data?
Evidently there IS something wrong with your program, but what can we do without data?
Since that's not a standard colorbar, I suggest you create an image with three colored separated boxes outlined in black on a white background, and stitch it to the bottom of your other image.
jonas
jonas on 28 Sep 2018
Edited: jonas on 28 Sep 2018
I agree with IA that it would be easier to help you if you upload your data. We could use some 'dummy' data but often times there are additional issue when adapting it to another set of data.
The obvious problem here is that colorbar position is given in normalized figure units while patch input is axes coordinates. With your approach I would use annotations instead, preferebly textboxes wit variable backgroundcolor.
I have upload the test file in the question.
jonas
jonas on 28 Sep 2018
Edited: jonas on 28 Sep 2018
Try something like this. The result is actually not too bad...
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
Yu Li
Yu Li on 28 Sep 2018
Edited: Yu Li on 28 Sep 2018
fantastic, thank you very much.
Please revise your first answer with this one, to let other people easier to find this answer. and I'll accept it after you revise it.
Happy to help! Yes, I was going to do that but wanted to wait until you were satisfied :)

Sign in to comment.

More Answers (0)

Categories

Find more on Images in Help Center and File Exchange

Asked:

on 28 Sep 2018

Edited:

on 28 Sep 2018

Community Treasure Hunt

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

Start Hunting!