Colorbar - axis ticks in Matlab

I am using below code

d3 = vals;
n = datesmonth;
figure
plot(n,d3);
colormap(jet(12));
hold on
plot(n, d3,'b-');
scatter(n, d3, [], RiskierInd, 'filled');
caxis([1 12]);
colorbar('YTick',[1:12],...
         'YTickLabels',{'Non-Durables','Durables','Manufacturing','Oil, Gas and Coal ','Chemicals','Technology','Telephone & TV','Utilities','Wholesale and Retail','Health','Finance','Other'})
datetick('x','mm-yyyy')

      

to create this figure

enter image description here

I have two quick questions:

  • Is it possible to center the rows of the row of the color row for each color? For example, the "Non-Durables" entry should not be at the bottom of the color bar, but in the middle of the darkest blue category.

  • Can I manually select colors for each category?

+3


source to share


2 answers


Rathbert pretty much answered the first question. However, for the sake of completeness, just do this and replace your current call caxis

:
caxis([0.5 12.5]);

      

To answer the second question, yes you can.

If you notice in your code, you've created a 12-component color map from a theme jet

. This creates a 12 x 3 matrix where each row is a unique color. Thus, if you want to manually select colors, you just need to change the order of the colors. If you look at the color bar label on your graph, the first color will start from the bottom or blue and it progresses to the top or red.

As a reference, this is the matrix created with jet(12)

:

>> cmap = jet(12)

cmap = 

     0         0    0.6667
     0         0    1.0000
     0    0.3333    1.0000
     0    0.6667    1.0000
     0    1.0000    1.0000
0.3333    1.0000    0.6667
0.6667    1.0000    0.3333
1.0000    1.0000         0
1.0000    0.6667         0
1.0000    0.3333         0
1.0000         0         0
0.6667         0         0

      

Each row contains a unique RGB tuple, where the first column is the amount of red, the second is green, and the third is the amount of blue. Therefore, the first pairs of colors are pure blue, and then tints of green are gradually added after that point to make it blue, etc. Etc.

The matrix is ​​arranged so that the first color is the first row and the last color is the last row. If you want to decide what colors you want for the label, you just need to arrange the lines so that they match the desired label.

Hence, you have a set of labels:

labels = {'Non-Durables','Durables','Manufacturing','Oil, Gas and Coal ','Chemicals','Technology','Telephone & TV','Utilities','Wholesale and Retail','Health','Finance','Other'};

      

... and currently you have the order of what color is displayed in the color map:



cmap = jet(12);
order = [1 2 3 4 5 6 7 8 9 10 11 12]; %// or order = 1:12;
cmap = cmap(order,:);

      

All you have to do is change order

it so you get the right color in the right order. So consult the color bar in your image and position each color so that it matches the same positions order

. For example, if you want to change the order of colors, you should do the following:

cmap = jet(12);
order = [12 11 10 9 8 7 6 5 4 3 2 1]; %// or order = 12:-1:1;
cmap = cmap(order,:);

      

Likewise, if you wanted yellow and blue to come first and the rest after, you would do:

cmap = jet(12);
order = [8 4 5 6 7 1 2 3 9 10 11 12];
cmap = cmap(order,:); 

      

After that, you call colormap

on cmap

and continue your plot:

%// From before
cmap = jet(12);
order = [4 5 6 8 7 1 2 3 9 10 11 12];
cmap = cmap(order,:); 

%// New
colormap(cmap);
hold on
plot(n, d3,'b-');
scatter(n, d3, [], RiskierInd, 'filled');
caxis([0.5 12.5]); %// Change
colorbar('YTick',[1:12],...
         'YTickLabels',{'Non-Durables','Durables','Manufacturing','Oil, Gas and Coal ','Chemicals','Technology','Telephone & TV','Utilities','Wholesale and Retail','Health','Finance','Other'})
datetick('x','mm-yyyy')

      


However, if you want to manually pick the colors yourself, this will be a little more active. You just need to know what colors you need and then put into the matrix. Remember that each color is an RGB tuple and fits on one line. You will need to take a look at the color picker, at least know that each component must be weighed to get the correct color.

Go here: http://colorpicker.com - You can select the color you want and write down the RGB values. Then divide each value by 255 and place it as an entry in the color map matrix. Each color represents a row, where the first column is red, the second column is green, and the third is blue. This is if you really want to control which color is appropriate for which category. You will need to immediately identify the correct combination of red, green, and blue values.

Good luck!

+1


source


For your first question, you can simply change:

caxis([0.5 12.5]);

      



I'm not sure to understand your second question, but I would say there is no easy way.

+1


source







All Articles