Reputation: 23
I would like to reproduce the following plot in MATLAB:
For instance, consider the following time series:
a= [1,0.5,0.25, -0.5, -0.75,0.5,1.25, -0.8,0.1,0.2,0,3,0.8, -0.9, -1,1]
How can I plot values above 0 in one color and values below another color?
Upvotes: 2
Views: 963
Reputation: 1550
The trick is to avoid the plotting of points by replacing their values by NaN.
I suggest to first separate a
in two arrays, lets say a_lo
and a_hi
.
a_lo
is a
but with positive values replaced by NaN
.a_hi
is a
but with negative values replaced by Nan
.So you will do something like this:
a_lo = a;
a_hi = a;
for i = 1:length(a)
if a_lo(i) > 0
a_lo(i) = NaN;
end
if a_hi(i) < 0
a_hi(i) = NaN;
end
end
Then you plot a_lo
and a_hi
with different colours, don't forget hold on
to plot the two curves together.
plot(a_lo,'r'); hold on;
plot(a_hi,'b');
Here is an example of what I could obtain with a sine wave:
Upvotes: 1