Dears, am plotting the response of an sensor with one curve in real time, and i want to plot this curve with two colors, depending on the values of the returned data, when data<500 the curve is plotted in "red" when data>=500 the curve is plotted in "green" so i need to plot one curve with two colors. best regards

 채택된 답변

Star Strider
Star Strider 2015년 1월 16일
편집: Star Strider 2015년 1월 16일

1 개 추천

There may be more efficient methods, but this works:
t = linspace(0, 3*pi, 500);
y = 200*sin(t) + 400;
ygidx = (y >= 500); % Define ‘Green’ Regions
tg = t;
yg = y;
yg(ygidx == 0) = NaN;
tg(ygidx == 0) = NaN;
figure(1)
plot(t, y, '-r')
hold on
plot(tg, yg, '-g')
hold off
You may have to experiment with it to get the result you want.

댓글 수: 2

gregor alexander
gregor alexander 2015년 1월 16일
thnx alot
Star Strider
Star Strider 2015년 1월 16일
My pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Kelly Kearney
Kelly Kearney 2015년 1월 16일

1 개 추천

If you plot with a patch rather than a line, you can get the color change. Depending on how much the y-values oscillate across the boundaries, you may need to upsample your data first to get a clean color transition (this particular example needed a lot of upsampling, since the values switch back and forth so much... a smoother curve won't need that):
x = 1:100;
y = rand(1,100)*1000;
xi = linspace(x(1), x(end), 5000);
yi = interp1(x,y,xi);
ci = yi < 500;
h = patch([xi NaN], [yi NaN], [ci NaN]);
set(h, 'edgecolor', 'interp');
colormap([0 1 0; 1 0 0]);

카테고리

도움말 센터File Exchange에서 Line Plots에 대해 자세히 알아보기

태그

질문:

2015년 1월 16일

댓글:

2015년 1월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by