R:从 x、y、z 绘制 3D 表面 [英] R: Plotting a 3D surface from x, y, z

查看:54
本文介绍了R:从 x、y、z 绘制 3D 表面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下我有一个 3 列的矩阵
x, y, z其中 z 是 x 和 y 的函数.

imagine I have a 3 columns matrix
x, y, z where z is a function of x and y.

我知道如何绘制这些点的散点图"plot3d(x,y,z)

I know how to plot a "scatter plot" of these points with plot3d(x,y,z)

但是如果我想要一个表面,我必须使用其他命令,例如surface3d问题是它不接受与 plot3d 相同的输入它似乎需要一个带有

But if I want a surface instead I must use other commands such as surface3d The problem is that it doesn't accept the same inputs as plot3d it seems to need a matrix with

(nº elements of z) = (n of elements of x) * (n of elements of x)

我怎样才能得到这个矩阵?我尝试过使用命令 interp,就像我需要使用等高线图时一样.

How can I get this matrix? I've tried with the command interp, as I do when I need to use contour plots.

如何在不计算此矩阵的情况下直接从 x、y、z 绘制曲面?如果我有太多点,这个矩阵就太大了.

How can I plot a surface directly from x,y,z without calculating this matrix? If I had too many points this matrix would be too big.

干杯

推荐答案

如果您的 x 和 y 坐标不在网格上,那么您需要将 x,y,z 表面插值到一个网格上.您可以使用任何地质统计学包(geoR、gstat 等)或更简单的技术(如反距离加权)通过克里金法来实现这一点.

If your x and y coords are not on a grid then you need to interpolate your x,y,z surface onto one. You can do this with kriging using any of the geostatistics packages (geoR, gstat, others) or simpler techniques such as inverse distance weighting.

我猜您提到的interp"功能来自 akima 包.请注意,输出矩阵与输入点的大小无关.如果需要,您可以在输入中包含 10000 个点并将其插入到 10x10 网格中.默认情况下 akima::interp 在 40x40 网格上执行此操作:

I'm guessing the 'interp' function you mention is from the akima package. Note that the output matrix is independent of the size of your input points. You could have 10000 points in your input and interpolate that onto a 10x10 grid if you wanted. By default akima::interp does it onto a 40x40 grid:

require(akima)
require(rgl)

x = runif(1000)
y = runif(1000)
z = rnorm(1000)
s = interp(x,y,z)
> dim(s$z)
[1] 40 40
surface3d(s$x,s$y,s$z)

因为它是随机数据,所以看起来很刺耳和垃圾.希望您的数据不是!

That'll look spiky and rubbish because its random data. Hopefully your data isnt!

这篇关于R:从 x、y、z 绘制 3D 表面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆