Personalising contour plot, map colour scheme

39 views (last 30 days)
Hi,
I have 2 sets of data, which when subtracted will give me the absolute performance of a system. Therefore there will be positive and negative values. These values are then plotted on a contour plot to understand which is best. I would like to personalise the colour bar with my own colour scheme as shown below:
-10 to -2: blue, -2 to 2: white, 2 to 10: red.
I would also like the colours to shade with change of magnitude - so the blue starts at dark blue from -10 to light blue to -2, then white between -2 and 2 and light red to dark red from 2 to 10.
I have already managed to plot the contour plot but need to change the colour map.
Matlab code
figure contourf(N_X3,N_Y3,N_Z3,50,'edgecolor','none') %N_X3 is the x axis 1x9 matrix N_Y3 is the y axis 7x1 matrix and N_Z3 is the z axis 7x9 matrix
title(d);
colormap cool %map colour style
xlabel('Peak Period (t)');
ylabel('Heading (deg)');
c = colorbar('southoutside');
c.Label.String = ('MSI%'); %This is how you label the colourbar

Accepted Answer

Neuropragmatist
Neuropragmatist on 5 Aug 2019
Edited: Neuropragmatist on 5 Aug 2019
Would something like this work for you?:
R = [linspace(0,1,100) ones(1,43) linspace(1,1,100)];
G = [linspace(0,1,100) ones(1,43) linspace(1,0,100)];
B = [linspace(1,1,100) ones(1,43) linspace(1,0,100)];
cmap = [R(:),G(:),B(:)]; %// create colormap
figure
img = peaks(128);
imagesc(img)
daspect([1 1 1])
colormap(cmap)
caxis([-10 10])
colorbar
% You might also want to look at this:
% colormapeditor
Basically every colormap can be described by a Mx3 matrix, so here I have used linspace to make a colormap that ranges from blue [0 0 1] to white [1 1 1] and then to red [1 0 0]. You can read more about color specification here. You can also see this in action if you run some code like this:
jet(256)
This generates a jet colormap with 256 elements and if you run this in the command window you will see that the output is a 256x3 matrix of color specifications.
You could also check out colormapeditor which lets you edit colormaps as you like:
Hope this helps,
M.
EDIT:
For contour the correct code would be this instead:
R = [linspace(0,1,100) ones(1,43) linspace(1,1,100)];
G = [linspace(0,1,100) ones(1,43) linspace(1,0,100)];
B = [linspace(1,1,100) ones(1,43) linspace(1,0,100)];
cmap = [R(:),G(:),B(:)]; %// create colormap
figure
img = peaks(128);
contour(img,128)
daspect([1 1 1])
colormap(cmap)
caxis([-10 10])
colorbar
And the result would look like this:
untitled.png
  2 Comments
Scikky
Scikky on 5 Aug 2019
Thanks!! after some minor editing it worked for my problem.
I have a very basic knowledge but I would like to learn more. So I have a question for you.
What I am wondering is why interpolate this many points: [linspace(0,1,100) ones(1,43) linspace(1,1,100)] ?
and how does the code know to keep -2 to 2 in white? or is that normal when making an axis from -10 to 10?
Thanks again!
Neuropragmatist
Neuropragmatist on 5 Aug 2019
You could use fewer points and it would just reduce the resolution of your colour axis. I used 100 points just so you could have a nice smooth colour bar but reduce the number if you wanted (243 colours is around the 256 mark most people use).
Matlab scales the colour axis to suit the data, so we could make a matrix 10000x3 and as long as the values can be interpreted as colours it would work as a colour axis. So what that means is just that the matrix we make should be proportional to the effect you want.
For instance, in the example I gave you there are 43 white points in the middle which is ~17.7% of the matrix or colour axis (43/243*100). You wanted white between -2 and 2 which is 20% of the space between -10 and 10 (4/20*100). That's why the scale works how you want.
You could make something far more complex and flexible, for instance you could ask the user how many points they want in the colour axis then calculate how many white points you need in the middle to maintain the correct effect. I was just doing this quickly :)
M.

Sign in to comment.

More Answers (0)

Products


Release

R2016a

Community Treasure Hunt

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

Start Hunting!