I have a time series X, which I can plot in x-y plane.
plot(X(:,1), X(:,2), 'r.')
This gives a red dot colour. What I want to do is I want to make this graph such that the colour represents the colour of the rainbow. (Red at the start, violet in the end) This is so we can give a time sense of the trajectory.
How do we do this?

 채택된 답변

Star Strider
Star Strider 2018년 4월 10일
편집: Star Strider 2018년 4월 11일

1 개 추천

Try this:
X = [(0:50)' sin((0:50)*pi/10)']; % Create ‘X’
cm = colormap(jet); % Approximates Spectrum
X(end,2) = NaN; % Set Last Value To ‘NaN’ To Create Line
c = flipud(X(:,1)); % Define Colours To Scale With ‘X(:,1)’
figure
patch(X(:,1), X(:,2), c, 'EdgeColor','interp')
EDIT Added plot image.

댓글 수: 6

CLARK KENDRICK GO
CLARK KENDRICK GO 2018년 4월 12일
Thanks for this. Now, I want to ask if there is a way to change from 'line' to 'dots'.
As in likes of plot(X(:,1),X(:,2),'r.') <-- red dots
You can
pointsize = 15;
scatter(X(:,1), X(:,2), pointsize, (1:size(X,1)).', '.');
colormap(jet)
Star Strider
Star Strider 2018년 4월 12일
@Walter — Thanks
Niklas Kurz
Niklas Kurz 2021년 1월 17일
Again just coming in clutch!
Niklas Kurz
Niklas Kurz 2021년 1월 17일
Would also be worth knowing how to apply the color map upside down, with other words going up the y-axis
%flip the current colormap
colormap(flipud(colormap))
or for fixed colormap
colormap(flipud(jet))

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2018년 4월 10일

0 개 추천

It is not possible to have multiple colors on a line create by a single call to plot() or the underlying function line() .
You can look in the File Exchange for a couple of contributions for drawing colored lines. One of them uses surface objects, and one of them uses patch objects.

카테고리

태그

Community Treasure Hunt

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

Start Hunting!

Translated by