Clear Filters
Clear Filters

Colorbar/varycolor ticks

6 views (last 30 days)
Janne
Janne on 15 Apr 2011
Answered: Irfan Mulla on 30 Jul 2016
Hi. I'm trying to plot several graphs in the same picture and then identify them by using a vertical colorbar. Each graph corresponds to one value and I'd like the colorbar to have as many colors as there are values and have the same value visible as numbers beside the bar.
The problem is that either only part of the values are shown or the values are in the wrong place. I'm trying to use varycolor.m here, but I'm not sure if it's necessary. Here's what I've tried:
figure(1)
ColorSet = varycolor(length(energia2(:,1)));
set(gca, 'ColorOrder', ColorSet);
hold all;
for k=1:length(energia2(:,1))
plot(thetadeg,M_nostd(:,k))
end
legend off
set(gcf, 'Colormap', ColorSet);
}
% This one prints all the values in the lower part of the colorbar and leaves the rest without numbers (the wanted numbers are from 0 to 5, while the length of the vector is 20)
%colorbar('YTick',energia2(:,1),'YTicklabel',energia2(:,1))
% This has the same problem
%colorbar('YTick',energia2(:,1))
% This one only prints half of the values, but prints them on the whole colorbar area (as I hoped)
colorbar('YTicklabel',energia2(:,1))
I don't understand how this should be done. How do I use colorbar the way I want?

Answers (2)

Sarah Wait Zaranek
Sarah Wait Zaranek on 19 Apr 2011
I believe you don't want to change the ticks but the limits of the axis that contains your colorbar:
h = colorbar; set(h,'Ylim',[0 5])
This will only show the values 0-5 (on the right scale) on your colorbar.
Note that this issue to similar to a question I answered earlier here: http://www.mathworks.com/matlabcentral/answers/5086-rescaling-colormap-colorbar
Edited to contain a difference example:
If you want to set the color order and corresponding colormap - I would suggest this way:
figure;
index = (0:0.25:5);
myMap = colormap(jet(length(index)));
set(0,'DefaultAxesColorOrder',myMap)
hold all
for k=1:length(index)
plot(rand(5,1))
pause(1)
end
hold off
h = colorbar;
set(h,'YTickLabel',num2str(index'));
  2 Comments
Janne
Janne on 19 Apr 2011
Thanks for assistance. That solves one problem I also had, but not the core. I have a vector 0:0.25:5 where each value gets its own graph in the figure. In the figure I can see all the graphs of different colors, but the problem is to get the colorbar to show these vector values corresponding to each color.
The last color should correspond to the value 5. With your code I can get the final value in the colorbar to be 5, but the colors don't match the actual graph values. For example, there are 20 different colored graphs, but the color bar scale shows only 4 different colors while still displaying the complete range of numbers, which means that the colors don't actually match the vector.
Sarah Wait Zaranek
Sarah Wait Zaranek on 19 Apr 2011
This I believe means you need to change the colormap that your colorbar is using - if I understand correctly. I will modify the answer to include that.
Cheers,
Sarah

Sign in to comment.


Irfan Mulla
Irfan Mulla on 30 Jul 2016
Well, one needs to just add a following line at the end of Jane's code
caxis([min(energia2(:,1)),max(energia2(:,1))])

Products

Community Treasure Hunt

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

Start Hunting!