Creating rainbow coloured plots in 3d

64 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
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
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 [-]';
view(3)
axis equal

DGM
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;
scatter3(x,y,z,10,z,'filled');
axis equal
grid on
colormap(jet)
... 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);
scatter3(xp,yp,zp,10,zp,'filled');
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.

Kategorien

Mehr zu Colormaps finden Sie in Help Center und File Exchange

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by