plotly - 不同表面的不同颜色 [英] plotly - different colours for different surfaces

查看:24
本文介绍了plotly - 不同表面的不同颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 plotly 我想让每个表面都有不同的颜色.

Using plotly I would like to have each surface to have different colour.

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)

p <- plot_ly(color = c("red", "blue")) %>%
     add_surface(x = df1$t1,
                 y = df1$t2,
                 z = df1$p1,
                 opacity = 0.8) %>%
     add_surface(x = df2$t1,
                 y = df2$t2,
                 z = df2$p2,
                 opacity = 1) %>%
     layout(autosize = F, width = 550, height = 550, margin = m,
            scene = list(xaxis = list(title = "Theta 1"),
                         yaxis = list(title = "Theta 2"),
                         zaxis = list(title = "P")),
            dragmode = "turntable")
p

很遗憾,我无法更改这两个表面的颜色.我尝试将 color = I("red")color = I("blue") 参数添加到 add_surface 但这只是改变了颜色两个表面的比例从红色到蓝色.

Unfortunately, I'm not able to change colours of these two surfaces. I tried to add color = I("red") and color = I("blue") arguments into add_surface but this just changed colour scale from red to blue for both surfaces.

我还尝试将 color = "red" 添加到 plot_ly() 并将 inherit = F 添加到第二个 add_surface.这仅将第一个表面更改为红色,但仅将黄色默认颜色更改为红色.我希望一个表面是红色的,第二个表面是蓝色的.

I also tried to add color = "red" into plot_ly() and add inherit = F into second add_surface. This changed the first surface only, but only the yellow default color into red. I would love to have one surface red and second one blue.

推荐答案

听起来微不足道,但在 Plotly 中有点棘手.曲面图的颜色要么来自 z 值,要么来自与 z 具有相同维度的数组.此颜色数组只接受数值,不接受颜色字符串或 RGB 值.

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)

接下来我们需要欺骗 Plotly 忽略色标.

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

这篇关于plotly - 不同表面的不同颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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