来自 python 网络图的 Dash Cytoscape 不遵守节点坐标
Dash Cytoscape from python networks graph not honouring the nodes coordinates
我正在尝试学习 Dash Cytoscape 并希望执行以下操作:
使用网络生成图形,应用 fruchterman-reingold 算法定位节点,将数据转换为 cytoscape 格式并将其显示在 Dash 应用程序中。这是代码:
""" from networkx
to Dash cytoscape
"""
import dash_cytoscape as cyto
from dash import Dash, html
import networkx as nx
SCALING_FACTOR = 100 # try using to scatter nodes
app = Dash(__name__)
# 1) generate a networkx graph
G = nx.les_miserables_graph()
# 2) apply a netowrkx layouting algorithm
pos = nx.fruchterman_reingold_layout(G, k=0.1, iterations=2000, threshold=1e-10)
# 3) convert networkx graph to cytoscape format
cy = nx.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 coords you got from (2) as coordinates of nodes in cy
for n, p in zip(cy["elements"]["nodes"], pos.values()):
n["pos"] = {"x": int(p[0] * SCALING_FACTOR), "y": int(p[1] * SCALING_FACTOR)}
# 6.) Take the results of (3)-(5) and write them to a list, like elements_ls
elements = cy["elements"]["nodes"] + cy["elements"]["edges"]
app.layout = html.Div(
[
cyto.Cytoscape(
id="cytoscape-layout-6",
elements=elements,
style={"width": "100%", "height": "800px"},
layout={"name": "preset"}, # "preset" to use the pos coords
)
]
)
if __name__ == "__main__":
app.run_server(debug=True)
我没有看到节点出现在它们应该出现的位置。事实上,它们似乎都在同一个坐标上折叠成一个节点。
希望可运行的代码注释将有助于理解我正在尝试做的事情。
先决条件包(environment.yml):
name: dash
channels:
- defaults
dependencies:
- pandas
- black
- plotly_express
- jupyterlab
- pylint
- pip
- python=3.8
- dash
- dash-html-components
- dash-core-components
- dash_cytoscape
- ipywidgets
- networkx
- colour
- matplotlib
谁能明白哪里出了问题?提前谢谢你
你试过了吗
n["position"] = {"x": int(p[0] * SCALING_FACTOR), "y": int(p[1] * SCALING_FACTOR)}
在第 5 步?
我正在尝试学习 Dash Cytoscape 并希望执行以下操作: 使用网络生成图形,应用 fruchterman-reingold 算法定位节点,将数据转换为 cytoscape 格式并将其显示在 Dash 应用程序中。这是代码:
""" from networkx
to Dash cytoscape
"""
import dash_cytoscape as cyto
from dash import Dash, html
import networkx as nx
SCALING_FACTOR = 100 # try using to scatter nodes
app = Dash(__name__)
# 1) generate a networkx graph
G = nx.les_miserables_graph()
# 2) apply a netowrkx layouting algorithm
pos = nx.fruchterman_reingold_layout(G, k=0.1, iterations=2000, threshold=1e-10)
# 3) convert networkx graph to cytoscape format
cy = nx.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 coords you got from (2) as coordinates of nodes in cy
for n, p in zip(cy["elements"]["nodes"], pos.values()):
n["pos"] = {"x": int(p[0] * SCALING_FACTOR), "y": int(p[1] * SCALING_FACTOR)}
# 6.) Take the results of (3)-(5) and write them to a list, like elements_ls
elements = cy["elements"]["nodes"] + cy["elements"]["edges"]
app.layout = html.Div(
[
cyto.Cytoscape(
id="cytoscape-layout-6",
elements=elements,
style={"width": "100%", "height": "800px"},
layout={"name": "preset"}, # "preset" to use the pos coords
)
]
)
if __name__ == "__main__":
app.run_server(debug=True)
我没有看到节点出现在它们应该出现的位置。事实上,它们似乎都在同一个坐标上折叠成一个节点。
希望可运行的代码注释将有助于理解我正在尝试做的事情。
先决条件包(environment.yml):
name: dash
channels:
- defaults
dependencies:
- pandas
- black
- plotly_express
- jupyterlab
- pylint
- pip
- python=3.8
- dash
- dash-html-components
- dash-core-components
- dash_cytoscape
- ipywidgets
- networkx
- colour
- matplotlib
谁能明白哪里出了问题?提前谢谢你
你试过了吗
n["position"] = {"x": int(p[0] * SCALING_FACTOR), "y": int(p[1] * SCALING_FACTOR)}
在第 5 步?