本文介绍了将特定线添加到 Plotly Scatter3d() 图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Plotly Scatter3d() 图,我想在其中一些图之间画线.从物理上讲,我有一个网络,其中有一些节点与债券相连,我想绘制这些债券.我该怎么做?

I have a Plotly Scatter3d() plot, and I want to draw lines between some of these. Physically speaking, I have a network where I have some nodes connected with bonds, and I want to draw these bonds. How do I go about it?

Scatter3d() 带有一个 mode='lines+markers' 选项,它只是使它成为点线散点图,而不是默认的点-只有情节.那不是我要找的.我想提供一个 xyz 坐标对列表,我想在最后提供一组线.

Scatter3d() comes with a mode='lines+markers' option that just makes it a point-and-line scatter plot, instead of the default point-only plot. That's not what I am looking for. I want to supply a list of pair of xyz coordinates, and I want a collection of lines at the end.

这是我绘制简单Scatter3d()绘图的函数:

Here is my function for plotting a simple Scatter3d()plot:

def Splot3dPlotly(xyz):
'''
3D scatter plot using Plotly.

:param xyz: (NPx3) array of xyz positions
:return: A Plotly figure that can now be plotted as usual.
'''
xyz = np.reshape(xyz, (int(xyz.size/3), 3))
NN = int(sqrt(xyz.shape[0]))

trace1 = go.Scatter3d(
    x=xyz[:,0],
    y=xyz[:,1],
    z=xyz[:,2],
    mode = 'markers', # 'lines+markers',
    marker=dict(color=range(NN*NN), colorscale='Portland')
            )

data = [trace1]
layout = go.Layout(
    margin=dict(
        l=0,
        r=0,
        b=0,
        t=0
    )
)

fig = go.Figure(data=data, layout=layout)
return fig

推荐答案

您可以在线条中添加第二条轨迹,每个坐标对由 None 分隔,以防止 Plotly 连接轨迹.

You could add a second trace with your lines and each coordinate pair is separated by None to prevent Plotly from connecting the traces.

import plotly.graph_objs as go
import plotly
plotly.offline.init_notebook_mode()

#draw a square
x = [0, 1, 0, 1, 0, 1, 0, 1]
y = [0, 1, 1, 0, 0, 1, 1, 0]
z = [0, 0, 0, 0, 1, 1, 1, 1]

#the start and end point for each line
pairs = [(0,6), (1,7)]

trace1 = go.Scatter3d(
    x=x,
    y=y,
    z=z,
    mode='markers',
    name='markers'
)

x_lines = list()
y_lines = list()
z_lines = list()

#create the coordinate list for the lines
for p in pairs:
    for i in range(2):
        x_lines.append(x[p[i]])
        y_lines.append(y[p[i]])
        z_lines.append(z[p[i]])
    x_lines.append(None)
    y_lines.append(None)
    z_lines.append(None)

trace2 = go.Scatter3d(
    x=x_lines,
    y=y_lines,
    z=z_lines,
    mode='lines',
    name='lines'
)

fig = go.Figure(data=[trace1, trace2])
plotly.offline.iplot(fig, filename='simple-3d-scatter')

这篇关于将特定线添加到 Plotly Scatter3d() 图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-08 16:02