Defining value range in colormap manually

My data range is 0-15
I plotted
scatter(X,Y,Data);
colormap([0 0 0; 1 0 0;0 0 1]);
it takes color 0 0 0 as 0-5 and so on
I need
0 0 0 as <4
1 0 0 as>4 and <9
0 0 1 as >9
How do I define value range in colormap

 Accepted Answer

Stephen23
Stephen23 on 11 Jan 2016
Edited: Stephen23 on 11 Jan 2016
You just need to supply the fourth option to scatter, which maps the values onto the colormap. One very easy way to do this is to use histc / histcounts:
X = rand(50,1);
Y = rand(50,1);
data = randi(16,50,1)-1;
[~,C] = histc(data,[0,4,9,Inf]);
scatter(X,Y,10,C)
colormap([0,0,0;1,0,0;0,0,1]);

7 Comments

Hello Stephen. Thanks for your reply, But I have X and Y and a corresponding Z, Z value 0 to infinity (say). I wish 0-4 as black, 4-9 as red and above 9 as blue in scatter format so as to say.
I am not so well versed in this art of Matlab, so I may be excused of this ignorance.
Stephen23
Stephen23 on 11 Jan 2016
Edited: Stephen23 on 11 Jan 2016
This is exactly what my code does: it colors the scatter points according to the data values, exactly as you have told us, using the three colors that you defined. Why do you think that it does not do this?
Hi Stephen, Thanks a ton for your answer
But histc makes the datasets as 1,2,and 3 corresponding to three ranges
When I use colorbar I get 1-3 instead of the real data values 0-infinity
Any solution friend?
Try specifying the colorbar tickmark locations and labels:
>> colorbar('YTicklabel',[0,4,9,Inf],'YTick',linspace(1,3,4))
Hello Stephen,
I am really grateful to you for your continual help. Thanks. I could get around it with your philanthropy.
My pleasure. You can also Accept answer that best resolve your question. This gives points to those who volunteer to help you.
If I want to set color limit for 3D plot,then what sort of changes I have to make? Like I want the red color for the Z axis value between 0.8 to 0.93, cyan color for Z axis value between 0.93 to 0.95, yellow color for Z axis value between 0.95 to 1 for given X and Y values. Please give me some guidance in this issue. Will be really be grateful!

Sign in to comment.

More Answers (1)

I'm not sure what you mean. All of data (if it's a 1-D array) will show up in the same color, and as circles with whatever markersize you specified as Data (which, by the way, I think is a bad name for you to choose as the marker size).
If you want multiple colors, you have to call scatter() twice.
Data = 20; % Circle diameter
% Define sample data.
X = 12 * rand(1,20);
Y = 12 * rand(1,20);
scatter(X,Y,Data);
% Define second sample data.
X = 12 * rand(1,20);
Y = 12 * rand(1,20);
hold on;
scatter(X,Y,Data);
legend('Set #1', 'Set #2');
grid on;
% colormap([0 0 0; 1 0 0;0 0 1]);
If you want, you can pass in an array that is the colormap for markers based on the index.
markerSize = 20; % Circle diameter
% Define sample data.
numPoints = 500;
X = 12 * rand(1, numPoints);
Y = 12 * rand(1, numPoints);
cMap = zeros(length(X), 3);
% Define markers to be red if both X and Y >4 and < 9
red = X > 4 & X < 9 & Y > 4 & Y < 9;
cMap(red, 1) = 1;
% Define markers to be blue if both X and Y >= 9
blue = X >= 9 & Y >= 9;
cMap(blue, 3) = 1;
scatter(X,Y, markerSize, cMap);
grid on;

5 Comments

Hi Thanks
Actually, for given X and Y pair I have a Z value (used Data for brevity) Z ranges from 0 to infinity(say) I wished scatter plots with just three colors 0-4 as black 4-9 as red and above 9 as blue.
Please Help.
Like I and Stephen area saying, you can't have Data as the third argument because if there's only 3 arguments the third argument must be the marker size. You have to supply a fourth argument if you want to make the marker a certain color. A slight variation from my second code example (to look at the magnitude of Z instead of X and Y) is:
markerSize = 20; % Circle diameter
% Define sample data.
numPoints = 100;
% Make up location data X and Y
X = 100 * rand(1, numPoints);
Y = 100 * rand(1, numPoints);
% Generate magnitude data Z
Z = 15 * rand(1, numPoints);
% Allocate colormap
cMap = zeros(length(X), 3);
% Define markers to be red if Z >4 and < 9
red = Z > 4 & Z < 9;
cMap(red, 1) = 1;
% Define markers to be red if Z >4 and < 9
blue = Z >= 9;
cMap(blue, 3) = 1;
% Now scatter the data with colors depending on Z magnitude.
scatter(X,Y, markerSize, cMap);
grid on;
Thanks Sir,
I was able to get around the issue with all support from you and Stephen. Take care.
If I want to set color limit for 3D plot,then what sort of changes I have to make? Like I want the red color for the Z axis value between 0.8 to 0.93, cyan color for Z axis value between 0.93 to 0.95, yellow color for Z axis value between 0.95 to 1 for given X and Y values. Please give me some guidance in this issue. Will be really be grateful!
I'm sure you made the obvious changes (something like)
% Define markers to be red if Z >= 0.8 and Z <= 0.93
redRows = Z >= 0.8 & Z <= 0.93;
cMap(redRows, :) = 1;
% Define markers to be cyan if Z >= 0.93 & Z <= 0.95
cyanRows = Z >= 0.93 & Z <= 0.95;
cMap(cyanRows, :) = [0, 1, 1];
but it didn't work, so you're asking me. I don't know what other changes you made in the code, which you forgot to post. Please post your non-working code along with your XYZ data in a .mat file with the paperclip icon.

Sign in to comment.

Categories

Asked:

on 11 Jan 2016

Commented:

on 12 Apr 2021

Community Treasure Hunt

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

Start Hunting!