Effacer les filtres
Effacer les filtres

Creating rainbow coloured plots in 3d

43 vues (au cours des 30 derniers jours)
Ganesh Aravind .S.E
Ganesh Aravind .S.E le 12 Nov 2022
Modifié(e) : DGM le 12 Nov 2022
I have values of z varying with respect to x, for various values of y. I've plotted them in 3d using plot3 command. I want to colour them in terms of their z values, as in like highest with the red, lowest with blue, similar to the image attached. Please help!
  1 commentaire
Star Strider
Star Strider le 12 Nov 2022
The turbo colormap most closely matches the visible spectrum.

Connectez-vous pour commenter.

Réponses (2)

Karim le 12 Nov 2022
This can be done with the patch command, see below for a demonstration
numPoints = 100;
% create a random line
Grid = zeros(numPoints,3);
Grid(:,1) = 10 * cos( 1:numPoints);
Grid(:,2) = 10 * sin( 1:numPoints);
Grid(:,3) = 1:numPoints;
% define some metric to determine the color
MyColor = sqrt( sum(Grid.^2 ,2) );
% create a connectivity matrix
Faces = [1:(numPoints-1); 2:numPoints]';
% create the figure
patch('Faces', Faces ,'Vertices', Grid ,'FaceColor', 'none', 'FaceVertexCData', MyColor ,'EdgeColor','interp' ,'LineWidth',5);
colormap jet
caxis([min(MyColor) max(MyColor)])
c = colorbar;
c.Label.String = 'Some Metric [-]';
axis equal

DGM le 12 Nov 2022
Modifié(e) : DGM le 12 Nov 2022
One common way to do this is by using a scatter plot. Unlike plot() or plot3(), scatter() and scatter3() can have their markers colormapped.
x = linspace(1,10,500);
y = 10*sin(0.5*x);
z = 0.1*x.^2;
axis equal
grid on
... but since there are no lines drawn between markers, you need enough point density that it appears as a solid line. If your data is not dense enough for satisfactory appearance, you may have to create an interpolated copy for the purpose of creating the plot.
% only 50 points
x = linspace(1,10,50);
y = 10*sin(0.5*x);
z = 0.1*x.^2;
% create interpolated copies
np = linspace(1,numel(x),numel(x)*10); % 500 points
xp = interp1(x,np);
yp = interp1(y,np);
zp = interp1(z,np);
axis equal
grid on
In 2D, you can also use surf() to create variable-colored lines.
Using patch() may also be an option, depending on the needs.


En savoir plus sur Colormaps dans Help Center et File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by