[英]matlab 3d surface plot from scatter3 data
I want to plot a 3d scatter plot with a surface plot on the same figure, so that I end up with something like this: 我想在同一个图上绘制一个带有曲面图的三维散点图,这样我最终会得到这样的结果:
I would have thought that the code below might have achieved what I wanted but obviously not. 我原以为下面的代码可能达到了我想要的但显然没有。 I have
x
, y
and z
data to plot a scatter3
: 我有
x
, y
和z
数据来绘制scatter3
:
x = [1 1 1 1 0.95 0.95 0.95 0.95 0.85 0.85 0.85 0.85 0.8 0.8 0.8 0.8 0.75 0.75 0.75 0.75]';
y = [0.3 0.2 0.15 0.1 0.3 0.2 0.15 0.1 0.3 0.2 0.15 0.1 0.3 0.2 0.15 0.1 0.3 0.2 0.15 0.1]';
z = [0.1 0.15 0.2 0.3 0.1 0.15 0.2 0.3 0.1 0.15 0.2 0.3 0.1 0.15 0.2 0.3 0.1 0.15 0.2 0.3]';
scatter3(x,y,z)
hold on
And now add a surface?? 现在添加一个表面?
p = [x y z];
surf(p)
Any ideas? 有任何想法吗? Thanks.
谢谢。
The problem is that surf is treating your matrix p as a 2D array of z values, with integer values for x & y. 问题是冲浪将矩阵p视为z值的二维数组,x和y的整数值。 Fortunately, surf has more than one way to enter values (see http://au.mathworks.com/help/matlab/ref/surf.html ).
幸运的是,surf有多种输入值的方法(参见http://au.mathworks.com/help/matlab/ref/surf.html )。
Try this: 尝试这个:
x = [1 1 1 1 0.95 0.95 0.95 0.95 0.85 0.85 0.85 0.85 0.8 0.8 0.8 0.8 0.75 0.75 0.75 0.75]';
y = [0.3 0.2 0.15 0.1 0.3 0.2 0.15 0.1 0.3 0.2 0.15 0.1 0.3 0.2 0.15 0.1 0.3 0.2 0.15 0.1]';
z = [0.1 0.15 0.2 0.3 0.1 0.15 0.2 0.3 0.1 0.15 0.2 0.3 0.1 0.15 0.2 0.3 0.1 0.15 0.2 0.3]';
xr = reshape(x, 4, 5);
yr = reshape(y, 4, 5);
zr = reshape(z, 4, 5);
surf(xr, yr, zr)
xlabel('x')
ylabel('y')
zlabel('z')
In this case, surf expects a 2D array of x, y, and z values (as they would appear if looking at them top down). 在这种情况下,surf需要一个x,y和z值的二维数组(如果从上到下看它们就会出现)。 This way, surf knows which vertices to connect into a surface.
这样,surf就知道要连接到表面的顶点。 Fortunately this can be easily achieved with your data by simply reshaping the vectors into matrices.
幸运的是,只需将矢量重新整形为矩阵,就可以轻松实现这一点。
In future, if all your points lie on the same x and y coordinates, you could replace xr with [1, 0.95, 0.85, 0.8, 0.75] and yr with [0.3, 0.2, 0.15, 0.1] and surf will convert them into arrays for you. 将来,如果你的所有点位于相同的x和y坐标上,你可以用[1,0,0.95,0.85,0.8,0.75]替换xr,用[0.3,0.2,0.15,0.1]替换yr,冲浪会将它们转换为数组给你。
I had to deal with the same problem; 我不得不处理同样的问题; I thought it might be still useful to answer this question
我认为回答这个问题可能仍然有用
You need to create a compatible z-axis/matrix.
您需要创建兼容的z轴/矩阵。 I wrote
scatt2surf
to convert scatter data to surfaces.我写了
scatt2surf
来将散布数据转换为曲面。 I hope it helps.我希望它有所帮助。
clear all;clc
xx = [1 1 1 1 0.95 0.95 0.95 0.95 0.85 0.85 0.85 0.85 0.8 0.8 0.8 0.8 0.75 0.75 0.75 0.75];
yy= [0.3 0.2 0.15 0.1 0.3 0.2 0.15 0.1 0.3 0.2 0.15 0.1 0.3 0.2 0.15 0.1 0.3 0.2 0.15 0.1];
zz= [0.1 0.15 0.2 0.3 0.1 0.15 0.2 0.3 0.1 0.15 0.2 0.3 0.1 0.15 0.2 0.3 0.1 0.15 0.2 0.3];
[x1,y1,z_surf]=scatt2surf(xx,yy,zz);
figure(3)
surf(x1,y1,z_surf);
xlabel('\it X');ylabel('\it Y');zlabel('\it Z');
colormap(hot)
hold on
scatter3(xx,yy,zz,[],zz,'o','filled','MarkerEdgeColor','none');
grid on;
function [x1,y1,z_surf]=scatt2surf(xx,yy,zz)
x1= unique(round(xx,7));
y1=unique(round(yy,7));
z_surf=ones(length(x1),length(y1));
for i=1:length(x1)
for j=1:length(y1)
indx=[xx==x1(i) & yy==y1(j)];
z_surf(i,j)=zz(indx);
end
end
z_surf=z_surf';
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.