微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将位置从 networkx 图形转换为虚线 cytoscape 空间

如何解决将位置从 networkx 图形转换为虚线 cytoscape 空间

我想基于邻接矩阵创建一个破折号 Cytoscape 图。为此,我实施了步骤 proposed by this user on the plotly forum生成可由 dash_cytoscape.Cytoscape 解释的元素列表。一切“工作正常”,我可以重现图表,只有位置似乎在错误的比例上。在 networkx 中,它们似乎在 -1 到 1 的范围内,而 Dash/Cytoscape 使用不同的坐标系。我该如何转换职位?

这是我的代码

import numpy as np
import networkx as nx
import dash
import dash_cytoscape as cyto
import dash_html_components as html

# create an adjancy matrix
A = np.array([[0,1,2,3],[1,0],[2,[3,0]],dtype=float)

# we can plot the adjancy matrix as a graph using networkx (we would like to see this exact same graph when we start the dash app)
G = nx.DiGraph(A)
nx.draw(G,with_labels=True)

enter image description here

def from_A_to_cy(A):
    '''Create a lists of elements that can be interpreted by dash_cytoscape.Cytoscape'''
    
    # 1.) create graph object
    G = nx.DiGraph(A)
    
    # 2 ) get node pos
    pos = nx.spring_layout(G)
    
    # 3.) get cytoscape layout
    cy = nx.readwrite.json_graph.cytoscape_data(G)
    
    # 4.) Add the dictionary key label to the nodes list of cy
    for n in cy['elements']['nodes']:
         for k,v in n.items():
             v['label'] = v.pop('value')
    
    # 5.) Add the pos you got from (2) as a value for data in the nodes portion of cy
    for n,p in zip(cy['elements']['nodes'],pos.values()):
        n['pos'] = {'x':p[0],'y':p[1]}
    
    # 6.) Take the results of (3)-(5) and write them to a list,like elements_ls
    elements_ls = cy['elements']['nodes'] + cy['elements']['edges']
    
    return elements_ls

# run dash and take in the list of elements
app = dash.Dash(__name__)

app.layout = html.Div([
    cyto.Cytoscape(
        id='cytoscape-two-nodes',layout={'name':'preset'},style={'width': '100%','height': '400px'},elements=from_A_to_cy(A)
    )
])

if __name__ == '__main__':
    app.run_server(debug=True)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。