define color range in scatter plot

Hi,
I have values ranging from 0 to 3000. Here, only for the values ranging between 2000 to 3000 I have to use the color palette, for rest of the values (i.e. 0 to 1000) the color should be in black..
And I am using scatter plot here.. Is it possible to define above conditions in the scatter plot..

 Accepted Answer

Jonas
Jonas on 29 Jun 2022
Edited: Jonas on 29 Jun 2022
i do not fully understand, you want to use the pallett for split to 0 to 3000, but at a certain threshold you want to use black instead?
what about values between 1000 and 2000?
x = linspace(0,3*pi,2000);
y = cos(x) + rand(1,2000);
c = linspace(1,10,length(x));
sp=scatter(x,y,[],c);
currMap=colormap();
[~,highestCDataIdx]=max(sp.CData(sp.XData<=3));
highestCDataIdx=ceil(highestCDataIdx*size(currMap,1)/numel(sp.XData)); % scale CData Index by ration of colormap elements to number of XData to map
currMap(1:highestCDataIdx,:)=0;
colormap(currMap)
you can also change values inbetween without changing the pallett
x = linspace(0,3*pi,2000);
y = cos(x) + rand(1,2000);
c = linspace(1,10,length(x));
sp=scatter(x,y,[],c);
currMap=colormap();
[~,highestCDataIdx]=max(sp.CData(sp.XData<=3));
highestCDataIdx=ceil(highestCDataIdx*size(currMap,1)/numel(sp.XData)); % scale CData Index by ration of colormap elements to number of XData to map
currMap(1:highestCDataIdx,:)=0;
colormap(currMap);
delInbetween=sp.XData<5 & sp.XData>4;
sp.XData(delInbetween)=[];
sp.YData(delInbetween)=[];
sp.CData(delInbetween)=[];

12 Comments

Thanks, Jonas..
This is what I am exactly looking at..
Actually,the values between 0 to 2000 should be in black and palette for the rest i.e. 2000 to 3000
so the full pallett for the rest or just parts of the palette?
or are you already happy with the given code?
clear
x = linspace(0,3000,20000);
y = 3*sin(2*pi*1/1000*x)+rand(1,20000);
c = linspace(1,10,length(x));
figure;
scatter(x,y,[],c);
figure;
sp=scatter(x,y,[],c);
currMap=colormap();
[~,highestCDataIdx]=max(sp.CData(sp.XData<=2000));
highestCDataIdx=ceil(highestCDataIdx*size(currMap,1)/numel(sp.XData)); % scale CData Index by ration of colormap elements to number of XData to map
currMap(1:highestCDataIdx,:)=0;
colormap(currMap);
figure;
y=y(x>2000);
x=x(x>2000);
c = linspace(1,10,length(x));
scatter(x,y,[],c);
xlim([0 3000])
linkaxes()
Thank you very much!!
Jonas,
Is there a way to fix particular color rather than a gradient , lets say
for 0 to 2 - green;
2 to 4 - red;
4 to 6 - yellow
something like this
Thanks !!
you can give each point its inidividual color, e.g.
clear
x = linspace(0,3,1000);
y = 3*sin(2*pi*1*x)+rand(1,1000);
c=zeros(numel(x),3);
firstColIdx=x<=1;
secColIdx=x<=2 & x>1;
thirdColIdx=x<=3 & x>2;
firstColor=[1 0.5 0];
secondColor=[0.5 0 0.5];
thirdColor=[0 1 0];
c(firstColIdx,:)=repmat(firstColor,[sum(firstColIdx),1]);
c(secColIdx,:)=repmat(secondColor,[sum(secColIdx),1]);
c(thirdColIdx,:)=repmat(thirdColor,[sum(thirdColIdx),1]);
figure;
scatter(x,y,[],c);
Jonas, actually in my case the varible c is not defined based on x, its rather different as shown in the herewith attachment..
i got a matrix with 4 columns. how to process this, how can i see what you want?
if you plot the 4 lines only with plot(yourMatrix), then your x is 1:size(yourMatrix,1)
Right now actually I am plotting like this.
As you can see I coloring based on entries in the matrix c. Since the selected colormap adopts gradient scale sometimes it is very difficult to differentiate..
figure ()
ax = gca;
scatter(x(:,1),y(:,1),0.2,c(:,1))
ylim ([0 3500]);
xlim ([0 1]);
%grid on
set(gca,'TickLabelInterpreter','latex')
set(gca, 'FontSize', 12)
title(sprintf("${Y=%3dmm}$",h),'FontSize',15,'Color','black','Interpreter','latex');
width=400;
height=300;
ax.LineWidth = 2;
set(gcf,'position',[x0,y0,width,height])
colormap (flipud(colorcube))
c2 = colorbar('FontSize',12,'TickLabelInterpreter', 'latex');
caxis ([-10 35]);
and how is the provided matrix connected to your code? i neither have your x, y and c nor does the provided 4 column matrix occur in your code
so you use the colormap colorcube which i do not have and how do you want to see the given plot?
Ah, Sorry.
I dont know why my previous attachment does not contains the x y c. You can find the same in this new attachment..
Actually I dont have any restriction with using colorcube, All I am looking for is colormap without gradient apparently colorcube is not.. So I am looking for another options..
The one you showed this morning is really promising, but I am having some while using my data !!
so some things to make the points in your cloud better distinctable: you can change the marker and the marker size and the coarseness of you colormap:
load('matlab.mat','x','y')
scatter(x,y,[],linspace(0,1,numel(x)),'.','SizeData',1)
colorbar;
cm=colormap('colorcube');
cm=cm(1:8:end,:);
colormap(cm)
or, if you want to use the supplied c for whatever reason
load('matlab.mat','c');
figure;
scatter(x,y,[],c,'.','SizeData',1)
colorbar;
cm=colormap('colorcube');
cm=cm(1:8:end,:);
colormap(cm)
Thank you very much, Jonas!!

Sign in to comment.

More Answers (1)

Community Treasure Hunt

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

Start Hunting!