plotly - different colours for different surfaces

只愿长相守 提交于 2019-11-29 10:37:15

Sounds trivial but it's a bit tricky in Plotly. The color of a surface plot is either derived from the z values or from an array with the same dimensions as z. This color array only accepts numerical values, no color strings or RGB values.

So let's define an array for our colors

color <- rep(0, length(df1$p1))
dim(color) <- dim(df1$p1)

Next we need to trick Plotly into ignoring the colorscale.

surfacecolor=color,
             cauto=F,
             cmax=1,
             cmin=0

et voilà, we have a uniformely colored plot.


library(plotly)
t1 <- seq(-3, 3, 0.1); t2 <- seq(-3, 3, 0.1)

p1 <- matrix(nrow = length(t1), ncol = length(t2))
p2 <- matrix(nrow = length(t1), ncol = length(t2))

p8a1 <- 1.2
p8a2 <- 1
p8d <- -1
p8b1 <- 0.7
p8b2 <- 0.6

for (i in 1:length(t2)) {
  for (j in 1:length(t1)) {
    p1[i, j] <- 1 / (1 + exp(-1.7 * (p8a1 * t1[j] + p8a2 * t2[i] + p8d)))
    p2[i, j] <- (1 / (1 + exp(-1.7 * p8a1 * (t1[j]- p8b1)))) * 
      (1 / (1 + exp(-1.7 * p8a2 * (t2[j]- p8b2))))
  }
}

df1 <- list(t1, t2, p1)
df2 <- list(t1, t2, p2)

names(df1) <- c("t1", "t2", "p1")
names(df2) <- c("t1", "t2", "p2")
m <- list(l = 10, r = 10, b = 5, t = 0, pad = 3)

color <- rep(0, length(df1$p1))
dim(color) <- dim(df1$p1)
p <- plot_ly(colors = c('red', 'blue')) %>%
  add_surface(x = df1$t1,
              y = df1$t2,
              z = df1$p1,
              opacity = 0.8,
              #surfacecolor=c('red')
              surfacecolor=color,
              cauto=F,
              cmax=1,
              cmin=0
  )
color2 <- rep(1, length(df2$p2))
dim(color2) <- dim(df2$p2 )

p <-  add_surface(p,
              x = df2$t1,
              y = df2$t2,
              z = df2$p2,
              opacity = 1,
              surfacecolor=color2,
              cauto=F,
              cmax=1,
              cmin=0)
p
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!