6

I am having a really hard time understanding how to change the range of my colorbar in Matlab2015b.

By default it will range from 0 to 1. I managed to change the label by using:

    c=colorbar;
    c.Limits=[0 180] % the range that I want

The problem is the colors don't scale when I do that, in other words it will display from 0 to 180 but still uses the colors associated to [0 1], which makes look the the whole bar like one color.

enter image description here

I used another approach by just changing the ticks and doing:

colorbar('Yticks',[0:10:180]).

Again, the colorbar is still associated to 0 to 1, so none of the ticks except 0 will appear as the first one starts at 10.

enter image description here

How do I change the data it is based on ? I tried changing c.UserData but it doesn't do anything.

M. Thompson
  • 95
  • 11
  • Could you post a sample of your code? I cannot reproduce this problem. My colorbar automatically uses the data range. – Schorsch Nov 11 '15 at 12:57
  • Sure, In this case I am really using the colorbar separately from the plot itself, it's not like I am plotting a surface and adding the colorbar to it. I am plotting a series of curves and I would like to use a colorbar instead of a legend (as there are many curves and it would look nicer this way). h=loglog(fliplr(PeTotal)/1e6,zTab/1000); %set of curves that I am plotting colorCode=jet(length(chiTab)); for k=1:length(chiTab) %length(chiTab) is just the number of curves set(h(k),'Color',colorCode(k,:)); end colormap(colorCode); colorbar; – Kevin Héritier Nov 11 '15 at 13:03

2 Answers2

2

From your comment, I see what you are trying to do.

You are on the right lines setting ytick, but as you noticed this only changes the position of the ticks on your colorbar, but the scaling stays the same. Instead, try to set yticklabel:

% Show the colorbar
c = colorbar;

% Define the desired ticks
ticks = [0:10:180];

% Sets the correct location and number of ticks
set(c, 'ytick', ticks / max(ticks));

% Set the tick labels as desired
set(c, 'yticklabel', ticks);
zelanix
  • 3,326
  • 1
  • 25
  • 35
1

A way that does not manually override tick specification can be achieved by:

limits = [0,180];
c = colorbar;
set(gca,'clim',limits([1,end]))

From there, the ticks may be manually changed or left alone as desired.

  1. Original scale colorbar
  2. Rescaled, but retained auto-tick labels

enter image description here enter image description here

M. Thompson
  • 95
  • 11