Vary color with height (z) for an rgl surface plot

无人久伴 提交于 2019-12-11 13:27:25

问题


I have a matrix of x,y, and z values (19,268 values) that are arranged as a surface. I plotted the surface using:

rgl::surface3d(mat$x, mat$y, mat$z, color="grey")

But, I want the color to vary with height (my Z values). The z-values range from -1.377385 to 29.93678.

How can I make my plot color vary with height?


回答1:


You haven't given a reproducible example, so I don't know if these results will be satisfactory, but the general idea is that the color argument can be an array of the same shape as z, and those colours will be used at corresponding locations.

For example,

library(rgl)
x <- y <- seq(-1, 1, len=20)
x <- x + 0.5 # to distinguish it from y
z <- outer(x, y, function(x,y) x^2 + y^2)
col <- rainbow(10)[cut(z, breaks = 10)]
surface3d(x, y, z, color = col)

This gives fairly rough edges to each colour change; you could improve it by using a finer grid, or more colours.



来源:https://stackoverflow.com/questions/50914631/vary-color-with-height-z-for-an-rgl-surface-plot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!