简体   繁体   English

2D矩阵的3D曲面图

[英]3D surface plot from 2D matrix

Using a 2d matrix in R, how can I create a 3d surface plot, where columns=x, rows=y and the values are the heights in z? 在R中使用2d矩阵,如何创建3d曲面图,其中columns = x,rows = y,值是z中的高度?

In the example below, x values would be 1:5, y value would be 1:5, and the numbers would represent heights/values in z. 在下面的示例中,x值为1:5,y值为1:5,数字表示z中的高度/值。

> m<-matrix(rnorm(25),nrow=5,ncol=5)
> m
             [,1]       [,2]       [,3]        [,4]       [,5]
[1,] -1.495513045  0.8000040 -1.1499261  1.65661138  0.8140510
[2,] -1.150018195 -0.7582933  0.8306922  0.16026908  0.3913198
[3,] -0.852609406  0.5525621  0.3585986 -0.45054768 -1.2259927
[4,] -0.001926297 -0.5857351  2.1518281  0.03192463  0.2065039
[5,] -1.641128610  0.4333973 -1.0616628 -0.92143426 -0.9598991

Package rgl should get you started... rgl应该让你开始......

require(rgl)
#  open renderer
open3d()
#  plot surface
rgl.surface( 1:10 , 1:10 , runif(100))
#  Export to png
rgl.snapshot( "sample.png" , fmt="png", top=TRUE )

在此输入图像描述

To get a 3D surface plot of a 2D matrix that contains the Z-values, try: 要获得包含Z值的2D矩阵的3D曲面图,请尝试:

require(plot3D)

persp3D(z = TheMatrix2D, theta = 120)

Use theta to adjust the viewing angle. 使用theta调整视角。 With my data, I get the following plot: 根据我的数据,我得到以下情节:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM