MATLAB: How to plot a graph where point colour relies on its value, based on several greater than and less than rules

plotplotting

I am trying to plot graphs where the points against the Y-axis determine the point itself to be one of five or more colours based on rules such as <0, >0 & <5, >5 & <42 etc.

Best Answer

Here's one way:
y = 10*rand(1, 150);
myColorMap = zeros(length(y), 3);
for k = 1 : length(y)
if y(k) < 3
myColorMap(k, :) = [1,0,0]; % Red
elseif y(k) < 6
myColorMap(k, :) = [1,0,1]; % Magenta
else
myColorMap(k, :) = [0,0,1]; % Blue
end
end
x = 1:length(y);
scatter(x, y, 20, myColorMap, 'filled');
grid on;
Related Question