![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/181162/image.png)
multiple colours in a trajectory plot
17 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
CLARK KENDRICK GO
el 10 de Abr. de 2018
Comentada: Walter Roberson
el 17 de En. de 2021
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?
0 comentarios
Respuesta aceptada
Star Strider
el 10 de Abr. de 2018
Editada: Star Strider
el 11 de Abr. de 2018
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')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/181162/image.png)
EDIT — Added plot image.
6 comentarios
Niklas Kurz
el 17 de En. de 2021
Would also be worth knowing how to apply the color map upside down, with other words going up the y-axis
Walter Roberson
el 17 de En. de 2021
%flip the current colormap
colormap(flipud(colormap))
or for fixed colormap
colormap(flipud(jet))
Más respuestas (1)
Walter Roberson
el 10 de Abr. de 2018
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.
0 comentarios
Ver también
Categorías
Más información sobre Blue en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!