![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/172465/image.png)
Defining value range in colormap manually
4 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Stephen23
2016-1-11
编辑:Stephen23
2016-1-11
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]);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/172465/image.png)
7 个评论
Stephen23
2016-1-12
My pleasure. You can also Accept answer that best resolve your question. This gives points to those who volunteer to help you.
Hasan Jamil Apon
2021-4-12
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!
更多回答(1 个)
Image Analyst
2016-1-11
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 个评论
Hasan Jamil Apon
2021-4-12
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!
Image Analyst
2021-4-12
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Red 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!