3 个简单的 2d 切片作为热图,其中数据来自 3d 中的公式

3 simple 2d slice in plotly as a heatmap with data from a formula in 3d

如何在作为热图的 3 维坐标系中拥有 3 个 2 维切片(x,y;x,z;y,z)。切片中的每个方块都应具有由公式输出给出的颜色。所以它应该看起来像这样,但在 plotly 或 R 中,并带有专门为颜色生成的数据(不是随机的):

您可以在 R 中使用 plotly 绘制曲面图,但据我所知,曲面将始终被插值:

假设您的 x、y 和 z 输出由三个不同的函数给出:

fun1 <- function(x, y) {
  sqrt(x^2 + y^2)
}

fun2 <- function(x, y) {
  rnorm(seq_along(x))
}

fun3 <- function(x, y) {
  x + y
}

然后您可以通过以下操作创建必要的输出:

xvals <- seq(0.01, 1, 0.01)
yvals <- seq(0.01, 1, 0.01)

df <- expand.grid(x = xvals, y = yvals)
df$f1 <- c(outer(xvals, yvals, fun1))
df$f2 <- c(outer(xvals, yvals, fun2))
df$f3 <- c(outer(xvals, yvals, fun3))

df$f1 <- ave(df$f1, floor(df$x * 10)/10, floor(df$y * 10)/10)
df$f2 <- ave(df$f2, floor(df$x * 10)/10, floor(df$y * 10)/10)
df$f3 <- ave(df$f3, floor(df$x * 10)/10, floor(df$y * 10)/10)

library(plotly)


plot_ly(df) %>%
 add_surface(z = matrix(0.75, 100, 100), x=matrix(df$x, 100, 100), 
             y = matrix(df$y, 100, 100), colorscale = 'Rainbow',
             surfacecolor = ~matrix(df$f1, 100, 100)) %>%
  add_surface(z = matrix(df$y, 100, 100), x = matrix(df$x, 100, 100), 
              y = matrix(0.5, 100, 100), colorscale = 'Rainbow',
              surfacecolor = ~matrix(df$f2, 100, 100)) %>%
  add_surface(z = ~matrix(df$y, 100, 100), x = matrix(0.5, 100, 100), 
              y = matrix(df$x, 100, 100), colorscale = 'Rainbow',
              surfacecolor = ~matrix(df$f3, 100, 100))