Conditional plotting, changing color of line based on value.

294 Ansichten (letzte 30 Tage)
S Tajik
S Tajik am 10 Feb. 2011
I've got a live stream of data coming to MATLAB. MATLAB does all the calculations and plotting just fine. The only problem is the colour of the plot lines.
I would like to have the plot line change colour automatically when is above or below a set number.
For example if my data is above 0 the plot line would be green and if below 0 the plot line would be red. So after a while when many data points were generated and plotted on the MATLAB figure, I'd like to see all lines above 0 to be green and all lines below 0 in red.
  1 Kommentar
Arijit
Arijit am 5 Mär. 2014
I like to mark in a stream time series data when data value equals to a threshold. Whenever such value is reached in a streaming time series plot the marker will appear and then streaming will go on with the marker. I have modified code from http://www.mathworks.in/matlabcentral/answers/87466-real-time-plot-from-streaming-data as:
t = 0 ; x = 0 ; startSpot = 0; interv = 1000 ; % considering 1000 samples step = 0.1 ; % lowering step has a number of cycles and then acquire more data while ( t <interv ) b = sin(t)+5; x = [ x, b ]; plot(x) ; if ((t/step)-500 < 0) startSpot = 0; else startSpot = (t/step)-500; end axis([ startSpot, (t/step+50), 0 , 10 ]); grid t = t + step; drawnow; pause(0.01) end
But this is not working as intended????

Melden Sie sich an, um zu kommentieren.

Antworten (6)

Seth DeLand
Seth DeLand am 10 Feb. 2011
Here's a way to do it that splits the data up into 2 lines and then plots them both. Overlapping points are set to NaN so that they are not plotted.
% Your Data
x = 0:0.01:10;
y = sin(x);
% Level for Color Change
lev = 0.2;
% Find points above the level
aboveLine = (y>=lev);
% Create 2 copies of y
bottomLine = y;
topLine = y;
% Set the values you don't want to get drawn to nan
bottomLine(aboveLine) = NaN;
topLine(~aboveLine) = NaN;
plot(x,bottomLine,'r',x,topline,'g');
  5 Kommentare
Walter Roberson
Walter Roberson am 11 Feb. 2011
plot(bottomLine,'r');
hold on;
plot(topline,'g')
Devendra Moharkar
Devendra Moharkar am 7 Sep. 2018
Hi, how can I use this code if i have like 3 thresholds, lets say i have data 0:10 and i want 1:3 to be green , 4:7 to be blue and 8:10 to be red?

Melden Sie sich an, um zu kommentieren.


Matt Fig
Matt Fig am 10 Feb. 2011
Perhaps you mean something like this:
x = -10:.01:10;
y = sin(x);
idx = y<=0;
plot(x(idx),y(idx),'r*',x(~idx),y(~idx),'b*')
EDIT
Simulate data coming in one piece at a time. Use a FOR loop as the simulated data retrieval mechanism.
x = -10:.01:10;
for ii = 1:length(x)
y(ii) = sin(x(ii)); % Data point ii has come in.
if y(ii)<0
c = 'r*';
else
c = 'b*';
end
plot(x(ii),y(ii),c)
hold on
end
  4 Kommentare
S Tajik
S Tajik am 10 Feb. 2011
data coming in live (numerical) and then used to do some calculations on it (find the high and low of a certain period).
once the calculations are done all the NEW data are stored in vectors. and feeded into the plot diagram. Ive been trying to use your method to do so but so far nothing. in your example you used y value as a condition, what I need is as follow:
if price>myIndicator
plot price green
if price<myIndicator
plot price red
hope that explains a bit more
thanks for the help.
Matt Fig
Matt Fig am 10 Feb. 2011
Notice I did not work on the whole vector with an IF statement! When I was working with the whole vector, I used a logical index instead. Look at the idx value in my first post and see how I used it.

Melden Sie sich an, um zu kommentieren.


Matt Tearle
Matt Tearle am 11 Feb. 2011
Maybe this is going overboard, but given your comment "but it only plots dots and do not connect them together", I'm guessing you're getting a lot of crossings of the cutoff value. So... here's a brute-force approach that actually interpolates between the crossings.
Because of what you said about size and memory, I tried to make it so that memory wouldn't be an issue. (So the code isn't as pretty as it probably could be.)
x = 0:0.1:10;
y = sin(4*x);
clr = {'b','r'};
lev = -0.35;
n = length(y);
idx = [0,find(sign(y(2:end)-lev)~=sign(y(1:end-1)-lev)),n];
nidx = length(idx);
for k=2:nidx
x1 = idx(k-1)+1;
x2 = idx(k);
line(x1:x2,y(x1:x2),'color',clr{mod(k,2)+1},'marker','o')
if k<nidx
x1 = x2;
x2 = x1+1;
y1 = y(x1);
y2 = y(x2);
x0 = x1 + (lev-y1)/(y2-y1);
y0 = y1 + (x0-x1)*(y2-y1);
line([x1,x0],[y1,y0],'color',clr{mod(k,2)+1})
line([x0,x2],[y0,y2],'color',clr{mod(k+1,2)+1})
end
end

Matt Tearle
Matt Tearle am 22 Feb. 2011
Because I'm a dork, I made a function to do this. It's now available on File Exchange. You can contact me to find out where to send generous tips!

Vieniava
Vieniava am 10 Feb. 2011
To control colour of any segment of plot you should use line()
>> doc line
  1 Kommentar
Walter Roberson
Walter Roberson am 10 Feb. 2011
That will not help any more than using plot(). line() has the same limitation, that any one line segment must be a single color.

Melden Sie sich an, um zu kommentieren.


MD FAZLE RABBI
MD FAZLE RABBI am 5 Feb. 2017
Hi Matt, If I have several thresholds, how can I plot them ??

Kategorien

Mehr zu Line Plots finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by