y = [0 8 12 8 0 -8 -12 -8 0];
x = [2 10 10 8 -2 -10 -10 -8 2];
figure,plot(x,y)
dx = diff(x);
heating = dx>0;
steady = dx ==0;
cooling = dx<0;
plotSegmentX = [x(1:end-1);x(2:end)];
plotSegmentY = [y(1:end-1);y(2:end)];
figure,plot(plotSegmentX(:,heating),plotSegmentY(:,heating),'r',...
plotSegmentX(:,cooling),plotSegmentY(:,cooling),'b',...
plotSegmentX(:,steady),plotSegmentY(:,steady),'g')
xlim([-14 14]);ylim([-12 12])
1 Comment
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/147331-change-plot-color-for-increasing-or-decreasing-x-values#comment_231109
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/147331-change-plot-color-for-increasing-or-decreasing-x-values#comment_231109
Sign in to comment.