MATLAB: Plotting negative values with blue and positive values with red

plotting

Hi,
I need to plot something similar to the attached picture out of 63X1 vector
I used so far the following:
plot(x(y<=0),y(y<=0), 'r', x(y>=0),y(y>=0), 'b')
but I need it to be vertical lines instead of one curve

Best Answer

You can use bar(). Try this:
% Create sample data.
numPoints = 63;
xValues = 1 : numPoints;
yValues = rand(1, numPoints) - 0.5;
% Find where data is positive or negative.
positiveIndexes = yValues >= 0;
negativeIndexes = yValues < 0;
% Plot column plot (bar chart).
bar(xValues(positiveIndexes), yValues(positiveIndexes), 'r', 'BarWidth', 1)
hold on
bar(xValues(negativeIndexes), yValues(negativeIndexes), 'b', 'BarWidth', 1)