Filter löschen
Filter löschen

Creating rainbow coloured plots in 3d

42 Ansichten (letzte 30 Tage)
Ganesh Aravind .S.E
Ganesh Aravind .S.E am 12 Nov. 2022
Bearbeitet: DGM am 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 Kommentar
Star Strider
Star Strider am 12 Nov. 2022
The turbo colormap most closely matches the visible spectrum.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Karim am 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 am 12 Nov. 2022
Bearbeitet: DGM am 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.


Mehr zu Colormaps finden Sie in Help Center und File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by