简体   繁体   中英

Plotly 3D surface plot not appearing

Hi I am trying to plot Plotly 3D surface plot, but unfortunately it doesnt appear. When I try with Scatter3D it works though not with Surface3D . Any ideas why?

# Scatter 3D
p = go.Figure()
p.add_trace(go.Scatter3d(
    x = df.X1,
    y = df.X2,
    z = df.Y3,
    mode = "markers",
    marker = dict(size = 3),
    name = "actual"
))

在此处输入图像描述

from plotly.offline import plot
trace = go.Surface(x = df.X1,y = df.X2,z = df.Y3)
data = [trace]
p2 = dict(data = data)
plot(p2)

在此处输入图像描述

A surface is not just a bunch of points. To draw a surface, Plotly needs to know how to split it in elementary triangles. Sure, you may think that, seeing your scatter plot, it seems obvious how to do so. But, well, it would be way less obvious if your points were not that planar. Plus, even in obvious cases, that would imply doing things like Delaunay's triangulation of all your (x,y) points. That would be costly.

So, well, in plotly at least, a surface is not just a bunch of points.

It is a matrix of z values, matching a regular, implicit mesh of (x,y) values.

Just see how you would draw a 3×3 plane surface with both methods (scatter and surface).

import plotly.graph_objects as go
p = go.Figure()
p.add_trace(go.Scatter3d(
    x = [0,1,2,0,1,2,0,1,2],
    y = [0,0,0,1,1,1,2,2,2],
    z = [1,1,1,1,1,1,1,1,1],
    mode = "markers",
    marker = dict(size = 3),
    name = "actual"
))
p.show()
import plotly.graph_objects as go
p = go.Figure()
p.add_trace(go.Surface(
    x = [0,1,2],
    y = [0,1,2],
    z = [[1,1,1],[1,1,1],[1,1,1]]
))
p.show()

In the second case, the surface is described as a 2D-array of z-values (x and y values just set the scale. You can omit them, unless you need irregular spacing)

In the first case, x,y,z together describe a bunch of points (that, in this example, happen to be regularly spaced, but plotly can't guess that, since nothing prevent me to give different values for x and y)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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