plot_ly 中的 z 参数究竟是什么? [英] What exactly is the z argument in plot_ly?

查看:72
本文介绍了plot_ly 中的 z 参数究竟是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个变量:xyz,我想生成一个曲面图.

z<-runif(50,0,1)y<-runif(50,1,2)x<-runif(50,3,6)plot_ly(x = ~x, y = ~y, z= ~z) %>% add_surface()

我收到以下错误

错误:`z` 必须是数字矩阵

z 如果不是垂直轴对应的变量,究竟代表什么?我看过 Volcano 示例,其中他们使用矩阵来生成该图,但我仍然不确定 z 矩阵在该示例中代表什么.

我希望有人使用 surface 绘制一个易于理解的 3D 函数,例如 z=f(x,y) = x^2 + y^2plot_ly 中的 code> 功能,以便我了解如何基于三个变量生成绘图.

解决方案

你上面代码的问题是,你没有指定跟踪类型 - 以及你需要传递给z 参数取决于此规范.

传递参数 x, y ,z 表明您想要显示 scatter3d 图 - 您可以通过删除 add_surface() 来测试:

z <- runif(50,0,1)y <- runif(50,1,2)x <- runif(50,3,6)plot_ly(x = x, y = y, z = z)

给出警告:

<块引用>

未指定跟踪类型:根据提供的信息,scatter3d"跟踪似乎合适.阅读有关此跟踪类型的更多信息 ->

# install.packages(listviewer")图书馆(情节)图书馆(列表查看器)架构(jsonedit = 交互())

我猜这就是你最初追求的:

z <- runif(50,0,1)y <- runif(50,1,2)x <- runif(50,3,6)plot_ly(x = x, y = y, z = z, type = 'mesh3d')

I have three variables: x,y, and z and I want to produce a surface plot.

z<-runif(50,0,1) 
y<-runif(50,1,2)
x<-runif(50,3,6)
plot_ly(x = ~x, y = ~y, z= ~z) %>% add_surface()

I get the following error

Error: `z` must be a numeric matrix

What exactly does z represent if not the variable corresponding to the vertical axis? I have seen the Volcano example where they use the matrix to generate that plot, but I still am not sure what that z matrix represents in that example either.

What I would like is for someone to plot an easy to understand 3D function like z=f(x,y) = x^2 + y^2 using the surface functionality in plot_ly just so I can understand how to generate a plot based on three variables.

解决方案

The problem with your above code is, that you haven't specified the trace type - and what you need to pass to the z argument depends on this specification.

Passing the arguments x, y ,z suggests you want to display a scatter3d plot - you can test this by dropping add_surface():

z <- runif(50,0,1) 
y <- runif(50,1,2)
x <- runif(50,3,6)
plot_ly(x = x, y = y, z = z)

Which gives the warning:

No trace type specified: Based on info supplied, a 'scatter3d' trace seems appropriate. Read more about this trace type -> https://plot.ly/r/reference/#scatter3d No scatter3d mode specifed:
Setting the mode to markers Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode

add_surface() on the other hand suggests you want to display a 3D Surface Plot. You already mentioned the volcano example. This kind of plot only needs a single numeric matrix to create the plot (argument z).

Accordingly with your example code you mixed up both plot types which leads to the error message.

How to avoid this confusion?

If you have a look at ?plot_ly there is a description for arguments "..." passed to the according trace type (z is one of them):

Arguments (i.e., attributes) passed along to the trace type. See schema() for a list of acceptable attributes for a given trace type (by going to traces -> type -> attributes).

schema() is a very useful hint to orient oneself in the plotly library. Execute the following code to browse through the different plotly trace types and their available attributes in a very convenient way:

# install.packages("listviewer")

library(plotly)
library(listviewer)
schema(jsonedit = interactive())

I guess this is what you were after in the first place:

z <- runif(50,0,1) 
y <- runif(50,1,2)
x <- runif(50,3,6)
plot_ly(x = x, y = y, z = z, type = 'mesh3d')

这篇关于plot_ly 中的 z 参数究竟是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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