绘图-多个表面的不同色标 [英] plotly - different color scales for multiple surfaces
问题描述
我正在尝试使用R 3.4.1的plotly(4.7.1)在同一图上绘制两个3D曲面,并使用两种不同的色标(一种是蓝色的变化,另一种是紫色的变化),和从z值得出的颜色值.当我分别绘制表面时,它工作得很好,但是当我使用add_trace或add_surface在同一图中绘制两个表面时,第二个表面将采用第一个表面的颜色.
I am trying to use plotly (4.7.1) with R 3.4.1 to plot two 3D surfaces on the same plot, with two different color scales (variations of blue for one and variations of purple for the other one), and color values derived from z values. When I plot surfaces separately, it works pretty fine, but when I plot both surfaces on the same plot by using add_trace or add_surface, the second surface takes the color of the first one.
这里是一个示例,使用来自 https:/的示例代码/plot.ly/r/3d-surface-plots/#new-to-plotly
Here is an example, using example code from https://plot.ly/r/3d-surface-plots/#new-to-plotly
library(plotly)
z <- c(
c(8.83,8.89,8.81,8.87,8.9,8.87),
c(8.89,8.94,8.85,8.94,8.96,8.92),
c(8.84,8.9,8.82,8.92,8.93,8.91),
c(8.79,8.85,8.79,8.9,8.94,8.92),
c(8.79,8.88,8.81,8.9,8.95,8.92),
c(8.8,8.82,8.78,8.91,8.94,8.92),
c(8.75,8.78,8.77,8.91,8.95,8.92),
c(8.8,8.8,8.77,8.91,8.95,8.94),
c(8.74,8.81,8.76,8.93,8.98,8.99),
c(8.89,8.99,8.92,9.1,9.13,9.11),
c(8.97,8.97,8.91,9.09,9.11,9.11),
c(9.04,9.08,9.05,9.25,9.28,9.27),
c(9,9.01,9,9.2,9.23,9.2),
c(8.99,8.99,8.98,9.18,9.2,9.19),
c(8.93,8.97,8.97,9.18,9.2,9.18)
)
dim(z) <- c(15,6)
z1 <- z - 1
z2 <- z + 1
cols1 <- c(rgb(255/255,112/255,183/255,1),rgb(128/255,0/255,64/255,1))
cols2 <- c(rgb(107/255,184/255,214/255,1),rgb(0/255,90/255,124/255,1))
p1 <- plot_ly(showscale = TRUE) %>%
add_surface(z = ~z1, cmin = min(z1), cmax = max(z2), color = ~z1, colors = cols1) %>%
layout(scene = list(zaxis = list(range = c(min(z1),max(z2)))))
p2 <- plot_ly(showscale = TRUE) %>%
add_surface(z = ~z2, cmin = min(z1), cmax = max(z2), color = ~z2, colors = cols2) %>%
layout(scene = list(zaxis = list(range = c(min(z1),max(z2)))))
p3 <- plot_ly(showscale = TRUE) %>%
add_surface(z = ~z1, cmin = min(z1), cmax = max(z2), color = ~z1, colors = cols1) %>%
add_surface(z = ~z2, cmin = min(z1), cmax = max(z2), color = ~z2, colors = cols2) %>%
layout(scene = list(zaxis = list(range = c(min(z1),max(z2)))))
p1
p2
p3
我尝试将inherit=F
设置为第二个add_surface
,但没有任何改变.我还查看了 plotly-不同表面的颜色不同,但是答案确实如此不适用于我的情况,因为我不需要均匀着色的图,而是取决于z值的颜色.
I tried inherit=F
into second add_surface
but it didn't change anything. I also looked at plotly - different colours for different surfaces, but the answer does not apply to my case as I don't want uniformely colored plots but colors depending on z values.
我没有在其他地方找到任何答案,但是我很陌生,所以我希望答案不明显.
我以某种方式设法通过在第二个add_surface
中使用colorscale
参数获得了两个不同的比例:第一个表面的颜色比例是默认值,第二个表面的颜色比例是不同的,但与我定义的颜色没有区别.
I didn't find any answers elsewhere, but I am pretty new to plotly so I hope the answer is not obvious.
I somehow managed to get two different scales by using the colorscale
argument in the second add_surface
: the first surface's colorscale was the default, and the second was different, but not from the colors I defined.
p4 <- plot_ly(showscale = TRUE) %>%
add_surface(z = ~z1, cmin = min(z1), cmax = max(z2), color = ~z1, reversescale=T) %>%
add_surface(z = ~z2, cmin = min(z1), cmax = max(z2), color = ~z2, colorscale = list(c(min(z1),"rgb(107,184,214)"),c(max(z2),"rgb(0,90,124)"))) %>%
layout(scene = list(zaxis = list(range = c(min(z1),max(z2)))))
p4
由于最后一个示例,我认为它在某种程度上是可以管理的,但是我想我在这里做错了.任何帮助将不胜感激!
I guess it is somehow manageable because of this last example, but I guess I am doing something wrong here. Any help would be very much appreciated!
推荐答案
我设法通过正确使用colorscale
参数来获得所需的结果,如以下代码所示:
I managed to get the result I wanted by using the colorscale
argument properly as in the following code:
p6 <- plot_ly(showscale = TRUE) %>%
add_surface(z = ~z1, cmin = min(z1), cmax = max(z2), colorscale = list(c(0,1),c("rgb(255,112,184)","rgb(128,0,64)"))) %>%
add_surface(z = ~z2, cmin = min(z1), cmax = max(z2), colorscale = list(c(0,1),c("rgb(107,184,214)","rgb(0,90,124)"))) %>%
layout(title="SURFACE 1 + SURFACE 2\n(Distinct colorscales as defined)", scene = list(zaxis = list(range = c(min(z1),max(z2)))))
它给出了以下内容: https://plot.ly/~sebastien1785/20/
但是,我无法通过使用colors
参数来重现此结果.谢谢您的帮助.
However, I was not able to reproduce this result by using the colors
argument. Thank you for your help.
这篇关于绘图-多个表面的不同色标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!