Custom made colormap for contourf

243 views Asked by At

I have a 3d function and want to show contours at [0.683 0.9545 0.9973]. I want the area 0->0.683 to be filled with red, 0.683->0.9545 to be blue, 0.9545->0.9973 to be green and 0.9973->1.0 to be white.

So basically I have the following:

contours = [0.0 0.683 0.9545 0.9973 1.0]
contourf(x,y,z,contours)

and tried

colormap([1 0 0; 0 0 1; 0 1 0; 1 1 1])

but it's not right. Please, what should my colormap look like in order to get the colours I want?

1

There are 1 answers

3
Dan On BEST ANSWER

At a guess, I would say that you need to have an amount of rows per color proportional to the size of each range. So 0->0.683 is a lot bigger than 0.683->0.9545. In your color map you have provided only one row each so Matlab assumes that those colors should be assigned equally over the full range (i.e. 0->1) which with 4 colors means that red is for 0->0.25, blue is for 0.25->0.5 etc...

Try something like this answer: How to create a custom colormap programmatically? but instead of using linspace, use repmat. You'll need to have enough rows to account for your 4 decimal point precision (which you might want to rethink) so in total you'll have 10 000 rows:

red = repmat([1 0 0], 6830, 1);
blue = repmat([0 0 1], 9545 - 6830, 1);
green = repmat([0 1 0], 9973 - 9545, 1);
white = repmat([1 1 1], 10000 - 9973 , 1);

map = [red;blue;green;white];
colormap(map);

or alternatively:

map = zeros(10000,3);
map(1:6830,1) = 1;
map(6831:9545,3) = 1;
map(9546:9973,2) = 1;
map(9974:end,:) = 1;