How to plot a surface in rgl plot3d

前端 未结 3 842
半阙折子戏
半阙折子戏 2021-01-16 05:12

So I have this code that produces the exact surface

f = function(x, y){
    z = ((x^2)+(3*y^2))*exp(-(x^2)-(y^2))
}
plot3d(f, col = colorRampPalette(c(\"blue         


        
相关标签:
3条回答
  • 2021-01-16 05:22

    I'm not sure to understand what you want. If my understanding is correct, here is a solution. Define a parametric representation of your surface:

    fx <- function(u,v) u
    fy <- function(u,v) v
    fz <- function(u,v){
      ((u^2)+(3*v^2))*exp(-(u^2)-(v^2))
    }
    

    Let's say you have these points:

    x0 <- seq(-3, 3, length.out = 20)
    y0 <- seq(-3, 3, length.out = 20)
    

    Then you can use the function parametric3d of the misc3d package, with the option fill=FALSE to get a wireframe:

    library(misc3d)
    parametric3d(fx, fy, fz, u=x0, v=y0, 
                 color="blue", fill = FALSE)
    

    Is it what you want?

    To get some vertical bars, use the function segments3d of rgl:

    i <- 8
    bar <- rbind(c(x0[i],y0[i],0),c(x0[i],y0[i],f(x0[i],y0[i])))
    segments3d(bar, color="red")
    
    0 讨论(0)
  • 2021-01-16 05:27

    Here is a plot with only 50 points using my original code.

    When I then apply what was said by Stéphane Laurent I then get this plot which feels too accurate when given the actual points I have

    Perhaps you need to explain to me what is actually happening in the function parametric3d

    0 讨论(0)
  • 2021-01-16 05:42

    You can do this by triangulating the surface. You don't give us your actual data, but I can create some similar data using

    f = function(x, y){
        z = ((x^2)+(3*y^2))*exp(-(x^2)-(y^2))
    }
    x <- runif(500, -3, 3)
    y <- runif(500, -3, 3)
    z <- f(x, y)
    

    Then the plotting is done using the method in ?persp3d.deldir:

    library(deldir)
    library(rgl)
    col <- colorRampPalette(c("blue", "white"))(20)[1 + round(19*(z - min(z))/diff(range(z)))]
    dxyz <- deldir::deldir(x, y, z = z, suppressMsge = TRUE)
    persp3d(dxyz, col = col, front = "lines", back = "lines")
    

    This might need some cosmetic fixes, e.g.

    aspect3d(2, 2, 1)
    

    After some rotation, this gives me the following plot:

    0 讨论(0)
提交回复
热议问题