Creating rainbow coloured plots in 3d

41 次查看(过去 30 天)
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!

回答(2 个)

Karim
Karim 2022-11-12
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 2022-11-12
编辑:DGM 2022-11-12
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.

类别

Help CenterFile Exchange 中查找有关 Orange 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by