我正在使用R编程语言。
下面是一个3D钟形曲线(高斯分布):
library(plotly)
library(mvtnorm)
mu <- c(0, 0)
Sigma <- matrix(c(1, 0, 0, 1), nrow=2)
x <- seq(-5, 5, length.out = 50)
y <- seq(-5, 5, length.out = 50)
grid <- expand.grid(x=x, y=y)
grid$z <- apply(grid, 1, function(x) dmvnorm(x, mean=mu, sigma=Sigma))
fig <- plot_ly(data = grid, x = ~x, y = ~y, z = ~z, type = 'scatter3d', mode = 'lines',
line = list(color = '#0066FF', width = 2)) %>%
layout(title = "3D Normal Distribution Wireframe Plot",
scene = list(xaxis = list(title = "X",
gridcolor = 'rgb(255, 255, 255)',
zerolinecolor = 'rgb(255, 255, 255)',
showbackground = TRUE,
backgroundcolor = 'rgb(230, 230,230)'),
yaxis = list(title = "Y",
gridcolor = 'rgb(255, 255, 255)',
zerolinecolor = 'rgb(255, 255, 255)',
showbackground = TRUE,
backgroundcolor = 'rgb(230, 230,230)'),
zaxis = list(title = "Z",
gridcolor = 'rgb(255, 255, 255)',
zerolinecolor = 'rgb(255, 255, 255)',
showbackground = TRUE,
backgroundcolor = 'rgb(230, 230,230)')),
showlegend = FALSE)
fig
字符串
x1c 0d1x的数据
**我的问题:**我想用x,y范围(-1到1)之间的许多红点填充这个图的内部。
我试着用下面的代码来做这件事:生成随机点,计算每个随机点的z值,然后把这些点放在图上:
points <- data.frame(x = runif(100, -1, 1), y = runif(100, -1, 1))
points$z <- apply(points, 1, function(x) dmvnorm(x, mean=mu, sigma=Sigma))
fig <- fig %>%
add_trace(data = points, x = ~x, y = ~y, z = ~z, type = 'scatter3d', mode = 'markers',
marker = list(color = 'red', size = 2))
fig <- fig %>%
layout(title = "3D Normal Distribution Wireframe Plot",
scene = list(xaxis = list(title = "X",
gridcolor = 'rgb(255, 255, 255)',
zerolinecolor = 'rgb(255, 255, 255)',
showbackground = TRUE,
backgroundcolor = 'rgb(230, 230,230)'),
yaxis = list(title = "Y",
gridcolor = 'rgb(255, 255, 255)',
zerolinecolor = 'rgb(255, 255, 255)',
showbackground = TRUE,
backgroundcolor = 'rgb(230, 230,230)'),
zaxis = list(title = "Z",
gridcolor = 'rgb(255, 255, 255)',
zerolinecolor = 'rgb(255, 255, 255)',
showbackground = TRUE,
backgroundcolor = 'rgb(230, 230,230)')),
showlegend = FALSE)
fig
型
的
但是点出现在图的表面上-我希望点出现在图的内部。有什么方法可以解决这个问题吗?
谢谢你,谢谢
1条答案
按热度按时间hgncfbus1#
你需要计算x,y,z空间中的许多随机点,然后过滤掉那些z值超过给定x,y点密度的点:
字符串
导致:
型
的数据
我个人认为,这给出了一个比体积情节更少的美学版本,我在之前的一个非常类似的问题中演示过