将等高线添加到3D地块 [英] Adding Contour Lines to 3D Plots

查看:14
本文介绍了将等高线添加到3D地块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用R编程语言。我使用";plotly";库进行了以下三维绘图:

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()

我现在正在尝试将等高线添加到上面的曲线图中,如下所示:https://plotly.com/r/3d-surface-plots/

我正在尝试改编来自";Ploly网站的代码以绘制这些轮廓,但我不确定如何做到这一点:

图表1:

# 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)
        )
      )
  )

图表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

有人能告诉我如何正确改编以上代码吗?

谢谢!

推荐答案

您马上就到了。
z上的等高线应根据zmin-max值定义:

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()

或由plotly自动设置:

plot_ly(x = input_1, y = input_2, z = z,
        contours = list(
          z = list(show = TRUE))) %>% 
        add_surface()

这篇关于将等高线添加到3D地块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆