Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 用plotly绘制三维网络图_Python 3.x_3d_Plotly_Plotly Python - Fatal编程技术网

Python 3.x 用plotly绘制三维网络图

Python 3.x 用plotly绘制三维网络图,python-3.x,3d,plotly,plotly-python,Python 3.x,3d,Plotly,Plotly Python,我正在尝试创建一个3D网络图,下面是代码片段 import networkx as nx import plotly.graph_objects as go from collections import OrderedDict from sympy.geometry import Point def distance_finder(s, t): p1, p2 = Point(s), Point(t) pmid = p1.midpoint(p2) return pmi

我正在尝试创建一个3D网络图,下面是代码片段

import networkx as nx
import plotly.graph_objects as go
from collections import OrderedDict
from sympy.geometry import Point


def distance_finder(s, t):
    p1, p2 = Point(s), Point(t)
    pmid = p1.midpoint(p2)
    return pmid.evalf()


def get_edge_trace(G):
    edge_x = []
    edge_y = []
    edge_z = []

    etext = [f'weight{w}' for w in list(nx.get_edge_attributes(G, 'weight').values())]
    xtext = [x]
    ytext = []
    ztext = []

    for edge in G.edges():

        x0, y0, z0 = G.nodes[edge[0]]['pos']
        x1, y1, z1 = G.nodes[edge[1]]['pos']

        mid = distance_finder(s=[x0, y0, z0], t=[x1, y1, z1])
        xtext.append(mid.x)
        ytext.append(mid.y)
        ztext.append(mid.z)

        edge_x.append(x0)
        edge_x.append(x1)
        edge_x.append(None)
        edge_y.append(y0)
        edge_y.append(y1)
        edge_y.append(None)
        edge_z.append(z0)
        edge_z.append(z1)
        edge_z.append(None)

    edge_trace = go.Scatter3d(
        x=edge_x, y=edge_y, z=edge_z,
        line=dict(width=0.5, color='#888'),
        mode='lines'
    )
    eweights_trace = go.Scatter3d(x=xtext, y=ytext, z=ztext, mode='text',
                              marker_size=0.5,
                              text=[0.45, 0.7, 0.34],
                              textposition='top center',
                              hovertemplate='weight: %{text}<extra></extra>')
    return edge_trace, eweights_trace


def get_node_trace(G):
    node_x = []
    node_y = []
    node_z = []
    for node in G.nodes():
        x, y, z = G.nodes[node]['pos']
        node_x.append(x)
        node_y.append(y)
        node_z.append(z)

    node_trace = go.Scatter3d(
        x=node_x, y=node_y, z=node_z,
        mode='markers',
        marker_size=12,
        #hoverinfo='text', #THIS LINE HAS NO SENSE BECAUSE text WAS NOT DEFINED
        marker_color='RoyalBlue',
        )

    return node_trace


if __name__ == '__main__':

    tail = [1, 2, 3]
    head = [2, 3, 4]

    xpos = [0, 1, 2, 3]
    ypos = [0, 0, 0, 0]
    zpos = [-1, 0, -9, 10]

    w = [1, 2, 3]
    v = [.1, 0.2, 0.3]
    xpos_ypos_zpos = [(x, y, z) for x, y, z in zip(xpos, ypos, zpos)]

    ed_ls = [(x, y) for x, y in zip(tail, head)]
    G = nx.OrderedGraph()
    G.add_edges_from(ed_ls)

    pos = OrderedDict(zip(G.nodes, xpos_ypos_zpos))
    edge_w = OrderedDict(zip(G.edges, w))
    edge_v = OrderedDict(zip(G.edges, v))
    nx.set_node_attributes(G, pos, 'pos')
    nx.set_edge_attributes(G, edge_w, 'weight')
    nx.set_edge_attributes(G, edge_v, 'value')

    # convert to plotly graph
    edge_trace, eweights_trace = get_edge_trace(G)
    node_trace = get_node_trace(G)

    fig = go.Figure(data=[edge_trace, node_trace, eweights_trace],
                    layout=go.Layout(
                        title='<br>Network graph made with Python',
                        titlefont_size=16,
                        showlegend=False,
                        hovermode='closest',
                        margin=dict(b=20, l=5, r=5, t=40),
                        xaxis_visible=False,
                        yaxis_visible=False)
                    )
    fig.write_html('plot.html', auto_open=True)
将networkx导入为nx
导入plotly.graph_对象作为go
从集合导入订单
从sympy.geometry导入点
def测距仪(s、t):
p1,p2=点(s),点(t)
pmid=p1.中点(p2)
返回pmid.evalf()
def get_edge_跟踪(G):
边缘_x=[]
边缘_y=[]
边缘_z=[]
etext=[f'weight{w}表示列表中的w(nx.get_edge_attributes(G,'weight').values())]
xtext=[x]
ytext=[]
ztext=[]
对于G.边()中的边:
x0,y0,z0=G.节点[边缘[0]][pos']
x1,y1,z1=G.节点[边[1]][pos']
mid=测距仪(s=[x0,y0,z0],t=[x1,y1,z1])
xtext.append(mid.x)
ytext.append(mid.y)
ztext.append(mid.z)
边附加(x0)
边附加(x1)
边缘附加(无)
边y.追加(y0)
边y.附加(y1)
边缘附加(无)
边缘附加(z0)
边_z.附加(z1)
边缘附加(无)
边缘_轨迹=go.Scatter3d(
x=边x,y=边y,z=边z,
线条=笔迹(宽度=0.5,颜色='#888'),
模式行'
)
eweights_trace=go.Scatter3d(x=xtext,y=ytext,z=ztext,mode='text',,
标记大小=0.5,
text=[0.45,0.7,0.34],
textposition='top center',
hovertemplate='weight:%{text}')
返回边跟踪,eweights跟踪
def get_节点_跟踪(G):
节点_x=[]
节点_y=[]
节点_z=[]
对于G.nodes()中的节点:
x、 y,z=G.nodes[node]['pos']
节点x.append(x)
节点_y.追加(y)
节点附加(z)
node_trace=go.Scatter3d(
x=节点x,y=节点y,z=节点z,
mode='markers',
标记大小=12,
#hoverinfo='text',#此行没有意义,因为未定义文本
马克笔颜色为蓝色,
)
返回节点跟踪
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
尾部=[1,2,3]
头部=[2,3,4]
xpos=[0,1,2,3]
ypos=[0,0,0,0]
zpos=[-1,0,-9,10]
w=[1,2,3]
v=[.1,0.2,0.3]
xpos_ypos_zpos=[(x,y,z)表示zip中的x,y,z(xpos,ypos,zpos)]
ed_ls=[(x,y)代表拉链中的x,y(尾部,头部)]
G=nx.OrderedGraph()
G.从(ed_ls)添加边
pos=订单数据通信(zip(G.nodes,XPO_YPO_zpos))
边缘w=有序的边缘(拉链(G.边缘,w))
edge_v=订购的DICT(拉链(G.边,v))
nx.设置节点属性(G,pos,'pos')
nx.设置边属性(G,边w,“权重”)
nx.set_edge_属性(G,edge_v,“value”)
#转换成绘图
边缘跟踪,eweights跟踪=获取边缘跟踪(G)
节点跟踪=获取节点跟踪(G)
fig=go.Figure(数据=[边缘跟踪,节点跟踪,eweights跟踪],
布局=go.layout(
title=“
用Python制作的网络图”, titlefont_尺寸=16, showlegend=False, hovermode='closest', 保证金=dict(b=20,l=5,r=5,t=40), xaxis_visible=False, yaxis_可见=假) ) 图write_html('plot.html',auto_open=True)
不幸的是,我犯了一个错误

图write_html('plot.html',auto_open=True) 返回pio.write_html(self、*args、**kwargs) fig_dict.get(“数据”),cls=utils.PlotlyJSONEncoder,sort_keys=True encoded_o=super(PlotlyJSONEncoder,self)。encode(o)。。。 raise TypeError(类型为{o.类的对象名称} TypeError:类型为Float的对象不可JSON序列化

我不知道为什么会发生这个错误。有人能帮我吗