问题
I am using the code below
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 produce this figure

I have two quick questions:
Is it possible to center the color bar string entries for each color? For example, the entry "Non-Durables" should not be at the bottom of color bar, but in the middle of the darkest blue category.
Is it possible to manually choose the colors for each category?
回答1:
Ratbert pretty much answered the first question. However, for the sake of completeness, simply do this and replace your current caxis
call:
caxis([0.5 12.5]);
To answer the second question, yes you can.
If you notice in your code, you produced a colour map of 12 components from the jet
theme. This produces a 12 x 3 matrix where each row is a unique colour. As such, if you want to manually choose the colours, you simply have to rearrange what order the colours come in. If you look at the colour bar label in your plot, the first colour starts from the bottom, or blue, and it progresses to the top, or red.
As a reference, this is the matrix that gets produced by 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 denotes the amount of red, second the amount of green and third the amount of blue. Therefore, the first couple of colours are purely blue, and then shades of green are incrementally added after that point to make it cyan and so on and so forth.
The matrix is arranged such that the first colour is the first row and the last colour is the last row. If you want to decide which colours you want for which label, you simply need to re-arrange the rows so that it matches whichever label you want.
Therefore, 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 an order of which colour appears in the colour 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
so that you get the right colour to appear in the right order. Therefore, consult the colour bar in your image, and make the positions of each colour be arranged so that it conforms to the same positions of order
. For example, if you wanted to reverse the colour ordering, you would do this:
cmap = jet(12);
order = [12 11 10 9 8 7 6 5 4 3 2 1]; %// or order = 12:-1:1;
cmap = cmap(order,:);
Similarly, if you wanted the yellow and cyan colours to come first and the rest to come after, you would do:
cmap = jet(12);
order = [8 4 5 6 7 1 2 3 9 10 11 12];
cmap = cmap(order,:);
Once you do this, you call colormap
on cmap
and continue with 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 choose the colours yourself, that'll be a bit more involved. You'd just need to know what colours you'd want then place them into the matrix. Remember, each colour is a RGB tuple and is placed in a single row. You'd have to look at a colour picker though to know what each component should be weighted as to get the right colour.
Go here: http://colorpicker.com - You can choose the exact colour you want and record the RGB values. After, divide each value by 255 and place this as an entry in your colour map matrix. Each colour is a row where the first column is the red, second column green and third column blue. This is if you truly want to control what colour goes with what category. You'll have to get right down to determining the right combination of red, green and blue values.
Good luck!
回答2:
For your first question, you can simply modify:
caxis([0.5 12.5]);
I'm not sure to understand your second question, but I'd say that there is no simple way.
来源:https://stackoverflow.com/questions/30289443/colorbar-axis-ticks-in-matlab