sta*_*oob 4 3d r data-visualization plotly
我正在使用 R 编程语言。我使用“plotly”库制作了以下 3 维图:
library(dplyr)
library(plotly)
my_function <- function(x,y) {
final_value = (1 - x)^2 + 100*((y - x^2)^2)
}
input_1 <- seq(-1.5, 1.5,0.1)
input_2 <- seq(-1.5, 1.5,0.1)
z <- outer(input_1, input_2, my_function)
plot_ly(x = input_1, y = input_2, z = z) %>% add_surface()
Run Code Online (Sandbox Code Playgroud)
我现在尝试将“轮廓线”添加到上面的图中,如下所示:https://plotly.com/r/3d-surface-plots/
我正在尝试调整“plotly 网站”中的代码来制作这些轮廓,但我不确定如何做到这一点:
图一:
# This might have worked?
fig <- plot_ly(z = ~z) %>% add_surface(
contours = list(
z = list(
show=TRUE,
usecolormap=TRUE,
highlightcolor="#ff0000",
project=list(z=TRUE)
)
)
)
fig <- fig %>% layout(
scene = list(
camera=list(
eye = list(x=1.87, y=0.88, z=-0.64)
)
)
)
Run Code Online (Sandbox Code Playgroud)
图2:
# I don't think this worked?
fig <- plot_ly(
type = 'surface',
contours = list(
x = list(show = TRUE, start = 1.5, end = 2, size = 0.04, color = 'white'),
z = list(show = TRUE, start = 0.5, end = 0.8, size = 0.05)),
x = ~x,
y = ~y,
z = ~z)
fig <- fig %>% layout(
scene = list(
xaxis = list(nticks = 20),
zaxis = list(nticks = 4),
camera = list(eye = list(x = 0, y = -1, z = 0.5)),
aspectratio = list(x = .9, y = .8, z = 0.2)))
fig
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我如何正确调整上述代码吗?
你就快到了。
上的轮廓z
应根据 的min
-max
值定义z
:
plot_ly(x = input_1, y = input_2, z = z,
contours = list(
z = list(show = TRUE, start = round(min(z),-2),
end = round(max(z),-2),
size = 100))) %>%
add_surface()
Run Code Online (Sandbox Code Playgroud)
或通过以下方式自动设置plotly
:
plot_ly(x = input_1, y = input_2, z = z,
colors = 'Oranges',
contours = list(
z = list(show = TRUE))) %>%
add_surface()
Run Code Online (Sandbox Code Playgroud)
等高线位于您的绘图上,但由于列表中的参数,可能不会非常明显contours.z
。以下是调整轮廓线以满足您的需求的方法:
fig <- plot_ly(z = ~z) %>% add_surface(
contours = list(
z = list(
show = TRUE,
# project=list(z=TRUE) # (don't) project contour lines to underlying plane
# usecolormap = TRUE, # (don't) use surface color scale for contours
color = "white", # set contour color
width = 1, # set contour thickness
highlightcolor = "#ff0000", # highlight contour on hover
start = 0, # include contours from z = 0...
end = 1400, # to z = 1400...
size = 100 # every 100 units
)
)
)
Run Code Online (Sandbox Code Playgroud)
x
您可以通过将列表传递给或 来沿其他维度绘制线条y
。(根据OP的后续问题)您可以使用 更改表面色标,指定指定的色标选项colorscale
之一或构建您自己的。例子:
fig <- plot_ly(z = ~z) %>% add_surface(
colorscale = "Picnic",
contours = list(
x = list(show=TRUE, color="#a090b0", width=2, start=0, end=30, size=7.5),
y = list(show=TRUE, color="#a090b0", width=2, start=0, end=30, size=7.5),
z = list(show=TRUE, color="#a090b0", width=2, start=0, end=1400, size=300)
)
)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1705 次 |
最近记录: |