2017-02-17 28 views
2

我有一個Plotly Scatter3d()陰謀,我想在其中一些之間畫線。從身體上講,我有一個網絡,我有一些節點與債券相連,我想繪製這些債券。我該如何解決它?將特定線條添加到Plotly Scatter3d()陰謀

Scatter3d()附帶一個mode='lines+markers'選項,該選項只是使其成爲點和線散點圖,而不是默認的僅限於點的圖。這不是我正在尋找的。我想提供一對xyz座標的列表,並且最後我想要一組線。

這裏是我的繪製簡單的Scatter3d()繪圖功能:

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 

回答

1

你可以用你的線條添加第二軌跡和每個座標對由None隔開,以防Plotly從連接的痕跡。

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') 

enter image description here

+0

我會接受這樣的答案,因爲沒有其他人,但Plotly應該很喜歡在包括繪製3D線條更輕鬆的方式。像matplotlib的LineCollection會是理想的。 – ap21

+0

@ ap21:我同意,加入'無'是相當危險的。我看到你在劇情論壇上要求這個功能,希望他們能聽到你的聲音! –