如何在matlab中获取3D散点图中的垂直线?

问题描述 投票:0回答:2

我有三个矩阵x,y,z,它们是通过matlab中的scatter3绘制的。但是,为了更好的可视化,我还需要从图中的每个点放下垂直线。

使用matlab 2017a,在matlab中实现了3D散点图。

enter code here
clc;
figure
x = [0,0,0,0,0,10,10,10,10,10];
y = [0,10,20,30,40,-10,0,10,20,30];
z = [46,52,51,59,53,85,56,87,86,88];
scatter3(x, y, z, 30, 'filled')
matlab plot 3d matlab-figure
2个回答
1
投票

正如@SardarUsama指出的那样,plot3应该做到这一点。代码可以更紧凑但保持清晰。

Vertical lines on 3d scatter plot

% MATLAB R2017a   
x = [0,0,0,0,0,10,10,10,10,10];
y = [0,10,20,30,40,-10,0,10,20,30];
z = [46,52,51,59,53,85,56,87,86,88];

figure
scatter3(x, y, z, 30, 'filled')  % scatter plot (3D)
zRng = zlim;
hold on
for k = 1:length(x)
    xL = [x(k) x(k)];
    yL = [y(k) y(k)];
    zL = [zRng(1) z(k)];
    plot3(xL,yL,zL,'r-')         % plot vertical line (3D)
end

2
投票

你也可以使用内置函数stem,它正是这样做的。

小技巧是你不能以简写形式z传递stem(x,y,z)坐标,但图形对象仍然接受z数据,你只需要将它们作为附加参数发送。

它的好处是你不需要循环;-)

x = [0,0,0,0,0,10,10,10,10,10];
y = [0,10,20,30,40,-10,0,10,20,30];
z = [46,52,51,59,53,85,56,87,86,88];

hp = stem(x,y,'filled','ZData',z) ;

或者正如Gnovice很好地指出的那样,更容易使用直接接受stem3数据的z函数:

hp = stem3(x,y,z,'filled') ;

以上两个例子都会产生:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.