我可以使用plotly仅绘制3D坐标吗?

时间:2019-01-04 01:05:43

标签: python plotly

如何绘制没有任何点的3D坐标? 我尝试在以下代码中将z = z_data.as_matrix()替换为z = [],但是我什么也没得到。

import plotly.offline as py
import plotly.graph_objs as go

import pandas as pd

# Read data from a csv
z_data = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/api_docs/mt_bruno_elevation.csv')

data = [
    go.Surface(
        z=z_data.as_matrix()
    )
]
layout = go.Layout(
    title='Mt Bruno Elevation',
    autosize=False,
    width=500,
    height=500,
    margin=dict(
        l=65,
        r=50,
        b=65,
        t=90
    )
)
fig = go.Figure(data=data, layout=layout)
py.plot(fig, filename='elevations-3d-surface')

1 个答案:

答案 0 :(得分:0)

要在Plotly中绘制3D坐标,您需要提供3个具有xyz坐标的输入列表/数据框/等。

data = [
    go.Scatter3d(
        x=val_x,
        y=val_y,
        z=val_z,
        mode='markers'
    )
]

您可以使用问题中链接的数据集并将其转换为这样的列表,例如

# Read data from a csv
z_data = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/api_docs/mt_bruno_elevation.csv')

# totally inelegant approach to convert the dataframe to a dictionary to convert it to a list
z_dict = z_data.to_dict()

val_x = []
val_y = []
val_z = []

for x in z_dict:
    for y in z_dict[x]:
        val_x.append(x)
        val_y.append(y)
        val_z.append(z_dict[x][y])

这将为您提供以下情节。

enter image description here

完整代码

import plotly.offline as py
import plotly.graph_objs as go
import pandas as pd

# Read data from a csv
z_data = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/api_docs/mt_bruno_elevation.csv')

# totally inelegant approach to convert the dataframe to a dictionary to convert it to a list
z_dict = z_data.to_dict()

val_x = []
val_y = []
val_z = []

for x in z_dict:
    for y in z_dict[x]:
        val_x.append(x)
        val_y.append(y)
        val_z.append(z_dict[x][y])

data = [
    go.Scatter3d(
        x=val_x,
        y=val_y,
        z=val_z,
        mode='markers'
    )
]
fig = go.Figure(data=data)
py.plot(fig, filename='elevations-3d-surface')