Clear Filters
Clear Filters

Specific color corresponding to specific data using contourf colormap

7 views (last 30 days)
I would like to know if we can give a specific color to a specific data and plot them on Matlab ?
For example, I have a 3000x3000 matrix with x and y axis, I need to plot a range of data in a specific color.
I've already generated the map with, but the colors are random
this is my code :
A= data;
[v,x] = meshgrid(v,x);
%colourRGB = hsv(jet);
c=colormap(jet); %jet
L=[0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1];
[cdd hc]=contourf(v,x,A,[-0.01 L]);
I want for example if A(x,y)==0.07 the colormap will be green ...
I can give more details.
Kind regards,
DGM on 13 May 2021
I'm still not really sure what you're aiming for, but consider:
% make some simple test data
x = linspace(0,1,100);
y = x.';
z = (x+y)/2;
% this is a simple colormap with 10 colors
% it looks pretty terrible, but they're distinct
% and recognizable
cm = [1 0 0;
1 1 0;
0 1 0;
0 1 1;
0 0 1;
1 0 1;
0.8 0 0;
0.8 0.8 0;
0 0.8 0;
0 0.8 0.8];
% the levels vector also has 10 levels
L=[0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1];
[cdd hc]=contourf(x,y,z,[-0.01 L]);
Now each band on the contour plot has a distinct color directly represented by the colormap. You could build whatever colormap you want to assign to each level. If you want to use jet or another built-in colormap the same way, specify the number of levels so that it lines up with your levels vector:
c=colormap(jet(10)); % or however many levels you have
Or you could modify a built-in map if you want to highlight certain things:
cm = jet(10); % use jet
cm(7,:) = [1.0000 0.2726 0.6517]; % but make one band pink
Is this even getting close to what you need?
S Ch
S Ch on 17 May 2021
thank you for all your answers, I'll check them later, I switched to another project with a deadline,
The best thing to confirm your response is to try them, so I don't want to accept, give me a moment, please.
Thanks again.

Sign in to comment.

Answers (2)

Chad Greene
Chad Greene on 13 May 2021
Perhaps @Kelly Kearney's contourfcmap is what you need?

S Ch
S Ch on 25 Jun 2021
Edited: S Ch on 25 Jun 2021
Thank you @DGM, @J. Alex Lee and @Chad Greene for your answers! It was great to use these methods and it is what I need!
I used the contourfcmap and I directly chose the number of levels I want, unfortunately I'm struggling to do a loop of 100 images using this method ! anyone can help me please?
To explain the problem, I have 100 matrices in a folder (each matrix is an image that I want to contourfmap) and after that save them in a folder as images with the colors I chose in contourfmap. Is this possible ?
This is my code:
for i = 1:N
%names is the char name of all matrices
if i < 10
names(i,:) = strcat("X_t0000",string(i),".mat");
names(i,:) = strcat("X_t000",string(i),".mat");
% A = eval(names);%(i,:)) ;
%After that I didn't know how to use names so I change to this follows:
Data= load(F(i).name);; %iside each matrix there is matrix named 'data'
h1 = contourfcmap(v,x,z,[0.1 0.33],jet(1),[.1 .1 .1], [.9 .9 .9], 'eastoutside')
d = h1.c
export_fig(fullfile(Folder, sprintf('%05d.png', i)))
%%% Here I had only one image appeared I think the last one.
% imwrite(d,fullfile(Folder, sprintf('%05d.png', i)))
% A = cat(3,names); %Here I wanted to concatenate the 100 matrices into
% one like 2048x2048x100 but I didn't succed
If it isn't clear please tell me to explain more. Thank you in advance.


Find more on Colormaps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!