0

I have data normalised beween 0 and 1 with continuous values inbetween. Is it possible to make a colour map to highlight one particular value? In this case, I want the max value of one to be a custom defined colour and the rest use a normal colour map. I have tried editing the colour map by appending on the desired colour but the plot uses this colour for values close to one as well. Any suggestions appreciated.

function cplotdemo

clc; close all; clear all;

DATA = peaks;
DATA = (DATA - min(DATA(:))) / ( max(DATA(:)) - min(DATA(:)) );

figure,
contourf(DATA)
colormap(jet2)
colorbar


function J = jet2(m)

if nargin < 1
    m = size(get(gcf,'colormap'),1);
end
n = ceil(m/4);
u = [(1:1:n)/n ones(1,n-1) (n:-1:1)/n]';
g = ceil(n/2) - (mod(m,4)==1) + (1:length(u))';
r = g + n;
b = g - n;
g(g>m) = [];
r(r>m) = [];
b(b<1) = [];
J = zeros(m+1,3);
J(r,1) = u(1:length(r));
J(g,2) = u(1:length(g));
J(b,3) = u(end-length(b)+1:end);

J(end,:)=[(256/2)/255,(0)/255,(256/2)/255];

I now think this is happening because of the way contourf works. For the example posted there should be only on value equal to one and that is the one I want to use the custom colour with.

Michael
  • 129
  • 1
  • 8

1 Answers1

0

Here is an example of creating a custom colormap that highlights a single level value. In this case the peak with the color black. You can adjust level to highlight any value in the surface and not just the peak.

% ==================================================
% DEFINE DATA
% --------------------------------------------------
[x,y] = meshgrid(-2:0.1:2, -2:0.1:2);
z = exp(-(x.^2+y.^2));
peak = max(z(:));

% ==================================================
% DEFINE LEVEL TO HIGHLIGHT AND ITS COLOR
% --------------------------------------------------
level = z(20,20);
color = [0,0,0];

cm = hsv;
nrows = size(cm,1);
row = round(nrows * level / peak);
cm(row,:) = color;

% ==================================================
% PLOT SURFACE WITH CUSTOM COLORMAP
% --------------------------------------------------
surf(x, y, z);
colormap(cm);

Here is the output figure

Example

Note, if you just want to highlight a particular pixel in the surface and not a value then you can define a color matrix using surf. Here is the relevant part from the docs:

surf(X,Y,Z,C) uses C to define color. MATLAB® performs a linear transformation on this data to obtain colors from the current colormap.

dpmcmlxxvi
  • 1,292
  • 1
  • 9
  • 13
  • Thank you. While this is looking a lot better, the problem persists as it is still labelling areas of .96, .99, etc with the specified colour. I guess I am trying to introduce a contour at a specific value. – Michael Aug 16 '15 at 18:31
  • @Michael I believe if you want to highlight based on a value you will always run into the problem you are having because MATLAB is linearly interpolating and rounding to the indices of the colormap. If you want to highlight specific pixels based on a unique value then I think you will have to identify the pixels you want to highlight then use the surf(x,y,z,c) alternative I mention at the end of my answer. – dpmcmlxxvi Aug 16 '15 at 18:34
  • @ dpmcmlxxvi, Thank you. I see what you mean. As a temporary measure I can plot a contour around the area of interest `contour(x,y,(z), [1 1], 'LineWidth', 2, 'Color', 'w','LineStyle','-');` – Michael Aug 16 '15 at 18:37