Colorbar/varycolor ticks

6 次查看(过去 30 天)
Janne
Janne 2011-4-15
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?

回答(2 个)

Sarah Wait Zaranek
Sarah Wait Zaranek 2011-4-19
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 个评论
Janne
Janne 2011-4-19
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 2011-4-19
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

请先登录,再进行评论。


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

类别

Help CenterFile Exchange 中查找有关 Purple 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by