问题
I am trying to plot a 3d surface by using plot_ly package.
x = c(1:10)
y = c(1:10)
z = matrix(runif(100),ncol = 10)
z_col = matrix(runif(100),ncol = 10)
plot_ly(x = ~x, y = ~y, z = ~z, surfacecolor =~ z_col,
type = "surface", colorscale = "Rainbow")
I expect the the color for the color bar will be like this:
However, this is what I get:
Can anyone show me how to show this problem ?
回答1:
From the documentation:
colorscale (colorscale)
Sets the colorscale. The colorscale must be an array containing arrays mapping a normalized value to an rgb, rgba, hex, hsl, hsv, or named color string. At minimum, a mapping for the lowest (0) and highest (1) values are required. For example,[[0, 'rgb(0,0,255)', [1, 'rgb(255,0,0)']].
You can define your own rainbow colorscale by using
colorscale = cbind(seq(0, 1, by=1/(length(z) - 1)), rainbow(length(z)))
which gives you
来源:https://stackoverflow.com/questions/46893717/colorscale-rainbow-in-plot-ly-doesnt-work