2017-02-17 24 views
2

私はPlotly Scatter3d()プロットを持っており、これらの間に線を描きたいと思います。物理的に言えば、私はいくつかのノードが債券に結びついているネットワークを持っており、これらの債券を描きたいと思います。それについてどうすればいいですか?Plotly Scatter3d()プロットに特定の線を追加する

Scatter3d()には、デフォルトの点のみのプロットではなく、ポイントアンドライン散布図を作成するオプションが付属しています(mode='lines+markers')。それは私が探しているものではありません。私はxyz座標のペアのリストを提供したい、そして私は最後に行のコレクションが欲しい。

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

あなたのラインと第2のトレースを追加することができ、各ペアは、トレースを結ぶからPlotly防ぐためにNoneによって分離されている座標:

は簡単なScatter3d()プロットをプロットするための私の関数です。

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:私は同意しますが、「なし」はかなりハッキリです。私はあなたがプロットのフォーラムでその機能を頼んだことを見ました、うまくいけば彼らはあなたを聞く! –

関連する問題