matlab plot three different colors for three different change (red, yellow, blue)
1 回表示 (過去 30 日間)
古いコメントを表示
In https://www.amcharts.com/demos/line-different-colors-ups-downs/ ,
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/166077/image.png)
it describes plotting different colors for up and downs. How can I do the same for matlab? I attach an example
plot([1 2 3 4 5 6 7 8 9 10], [5 5 7 5 2 5 5 8 9 2])
which involves no change as well. I want to have yellow for ups and blue for downs, and red for no change.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/166078/image.png)
0 件のコメント
回答 (2 件)
KSSV
2017 年 7 月 17 日
x = [1 2 3 4 5 6 7 8 9 10] ;
y = [5 5 7 5 2 5 5 8 9 2] ;
figure
hold on
for i = 1:length(x)-1
m = (y(i)-y(i+1))/(x(i)-x(i+1)) ;
if sign(m)==0
plot(x(i:i+1),y(i:i+1),'r') ;
elseif sign(m)==-1
plot(x(i:i+1),y(i:i+1),'b') ;
elseif sign(m)==1
plot(x(i:i+1),y(i:i+1),'y') ;
end
end
0 件のコメント
Stephen23
2017 年 7 月 17 日
編集済み: Stephen23
2017 年 7 月 17 日
This is MATLAB, so there is no need to use ugly loops or write complicated code. Simple is always best.
X = [1,2,3,4,5,6,7,8,9,10];
Y = [5,5,7,5,2,5,5,8,9,2];
D = [NaN,diff(Y),NaN]
idz = D(1:end-1)==0 | D(2:end)==0;
idp = D(1:end-1)>0 | D(2:end)>0;
idn = D(1:end-1)<0 | D(2:end)<0;
plot(X,Y+0./idz,'r', X,Y+0./idp,'y', X,Y+0./idn,'b')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/177457/image.png)
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で View and Analyze Simulation Results についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!