简体   繁体   English

了解 Plotly 中的 3D 轨迹

[英]Understanding 3D traces in Plotly

I am using the R programming language.我正在使用 R 编程语言。 I made the following 3D plot in R:我在 R 中制作了以下 3D 绘图:

# set seed for reproducibility 
#load libraries
    set.seed(123)
    library(dplyr)
    library(plotly)
    
#create data
    n <- 3
    my_grid <- expand.grid(i1 = 1:n, i2 = 1:n)
    my_grid$final_value = with(my_grid, sin(i1) + cos(i2) )
    
 
#make plot
       plot_ly() %>% 
        add_trace(data = my_grid,  x=my_grid$i1, y=my_grid$i2, z=my_grid$final_value, type='mesh3d') %>%
        add_surface(
            z = my_grid %>% as.matrix(),
            surfacecolor = my_grid,
            cauto=F,
            cmax=max(my_grid$final_value),
            cmin=min(my_grid$final_value)
        )

This produces the following plot:这会产生以下图:

在此处输入图片说明

As expected, this plot seems to be very logical : it shows a 3D surface where x = i1, y = i2, z = final_value, and the color of the plot is according "final_value".正如预期的那样,这个图似乎非常合乎逻辑:它显示了一个 3D 表面,其中 x = i1、y = i2、z = final_value,并且图的颜色根据“final_value”。

Problem: If I try to add some more data to the grid and then create the plot:问题:如果我尝试向网格添加更多数据,然后创建绘图:

#create more data
n <- 50
my_grid <- expand.grid(i1 = 1:n, i2 = 1:n)
my_grid$final_value = with(my_grid, sin(i1) + cos(i2) )


#make plot
plot_ly() %>% 
    add_trace(data = my_grid,  x=my_grid$i1, y=my_grid$i2, z=my_grid$final_value, type='mesh3d') %>%
    add_surface(
        z = my_grid %>% as.matrix(),
        surfacecolor = my_grid,
        cauto=F,
        cmax=max(my_grid$final_value),
        cmin=min(my_grid$final_value)
    )

在此处输入图片说明

Not only does this graph look "strange", but the "y-coordinate" (1697) within the hover text is displaying a value which is not present within the original data:该图不仅看起来“奇怪”,而且悬停文本中的“y 坐标”(1697)显示的值在原始数据中不存在:

#display histogram of values

par(mfrow=c(1,3))

 hist(my_grid$i1)
 hist(my_grid$i2)
 hist(my_grid$final_value)

在此处输入图片说明

Question: In the above histogram, a value of "1697" does not appear in any of the variables.问题:在上面的直方图中,值“1697”没有出现在任何变量中。 So how is it possible that such a large value is displayed within the plotly diagram?那么如何在绘图图中显示如此大的值呢?

Thanks谢谢

First of all you are mixing up two different trace types:首先,您正在混合两种不同的跟踪类型:

On the one hand mesh3d and on the otherhand a surface trace.一方面是mesh3d ,另一方面是表面跟踪。 Both can handle x, y, z coordinates.两者都可以处理 x、y、z 坐标。

However, you are passing a matrix as the z arguement of the surface trace (and no x and y values).但是,您正在传递一个矩阵作为表面轨迹的 z 参数(并且没有 x 和 y 值)。 Accordingly plotly is taking the values of the matrix into account for the z-axis and the dimensions of the matrix (2500 x 3) for y and x.因此,plotly 考虑了 z 轴的矩阵值和 y 和 x 的矩阵维度 (2500 x 3)。

Please also run schema() and navigate to:还请运行schema()并导航到:

object ► traces ► surface ► attributes对象 ► 痕迹 ► 表面 ► 属性

and

object ► traces ► mesh3d ► attributes对象 ► 痕迹 ► mesh3d ► 属性

to see the differences.看到差异。

With respect to your dataset I guess you want something like this:关于你的数据集,我猜你想要这样的东西:

# set seed for reproducibility 
#load libraries
set.seed(123)
library(dplyr)
library(plotly)

#create more data
n <- 50
my_grid <- expand.grid(i1 = 1:n, i2 = 1:n)
my_grid$final_value = with(my_grid, sin(i1) + cos(i2) )

#make plot
plot_ly(data = my_grid,  x=~i1, y=~i2, z=~final_value, type='mesh3d', intensity = ~final_value, colors = colorRamp(c("blue", "grey", "red")))

结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM