MATLAB: How to give data points different colors above and below a reference line

dual color plot

I have some data points that are plotted against the time. The variation goes positive and negative as the the time goes on. Taking zero as my refernce line I want to show a different color (say red) to those points which are below the reference line(negative)and some other color (say blue) to those points which are above the reference line.

Best Answer

Try this:
x = 0: 4*pi;
y = sin(x)
theValue = 0.5;
aboveLine = y > theValue; % or whatever.
% Plot the lines between the data in black.
plot(x, y, 'k-');
hold on;
% Plot the line itself.
line([x(1), x(end)], [theValue, theValue], 'Color', 'k');
% Plot above line in red.
plot(x(aboveLine), y(aboveLine), 'rd',...
'LineWidth', 2, 'MarkerSize', 10);
% Plot below line in blue.
plot(x(~aboveLine), y(~aboveLine), 'bs',...
'LineWidth', 2, 'MarkerSize', 10);