问题描述
我想在同一图上绘制一个3d散点图和一个表面图,以便得到这样的结果:
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:
我本以为下面的代码可能实现了我想要的,但显然没有实现.我有x
,y
和z
数据来绘制scatter3
:
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 = [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
现在添加一个曲面?
p = [x y z];
surf(p)
有什么想法吗?谢谢.
Any ideas? Thanks.
推荐答案
问题是surf将您的矩阵p视为z值的2D数组,其中x和amp; y.幸运的是,surf有多种输入值的方式(请参见 http://au. mathworks.com/help/matlab/ref/surf.html ).
The problem is that surf is treating your matrix p as a 2D array of z values, with integer values for x & y. Fortunately, surf has more than one way to enter values (see http://au.mathworks.com/help/matlab/ref/surf.html).
尝试一下:
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')
在这种情况下,surf期望x,y和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). This way, surf knows which vertices to connect into a surface. Fortunately this can be easily achieved with your data by simply reshaping the vectors into matrices.
将来,如果所有点都位于相同的x和y坐标上,则可以将xr替换为[1、0.95、0.85、0.8、0.75],将yr替换为[0.3、0.2、0.15、0.1],然后将它们转换为数组.
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.
这篇关于来自scatter3数据的matlab 3d表面图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!