简体   繁体   English

Python - 创建交互式网络图(例如 Plotly)

[英]Python - Create interactive network graph (ex. Plotly)

I am trying to create an interactive network graph using the likes of plotly or graphviz, but need some assistance?我正在尝试使用 plotly 或 graphviz 之类的方法创建交互式网络图,但需要一些帮助吗?

My data frame looks like this:我的数据框如下所示:

source        Target          Weight
Prod A        Prod F          0.56 
Prod F        Prod B          0.5 
Prod B        Prod J          0.64 
Prod B        Prod F          0.23 
Prod B        Prod F          0.9 
Prod A        Prod M          0.28 
Prod C        Prod M          0.5 
Prod Z        Prod A          0.45 

Can anyone help creating an interactive network graph from the above data set?任何人都可以帮助从上述数据集创建交互式网络图吗?

Thanks谢谢

The library d3graph can make an interactive graphs.d3graph可以制作交互式图形。 It uses d3js and html functionalities to make the interactive graph for the web-browser.它使用 d3js 和 html 功能来制作 Web 浏览器的交互式图形。 The results can also be shared with others.结果也可以与他人共享。

A demonstration:演示:

pip install d3graph

# Import the library
from d3graph import d3graph, vec2adjmat

# Example data
source = ['node A','node F','node B','node B','node B','node A','node C','node Z']
target = ['node F','node B','node J','node F','node F','node M','node M','node A']
weight = [5.56, 0.5, 0.64, 0.23, 0.9,3.28,0.5,0.45]

# Convert to adjacency matrix
adjmat = vec2adjmat(source, target, weight=weight)
print(adjmat)
# target  node A  node B  node F  node J  node M  node C  node Z
# source                                                        
# node A    0.00     0.0    5.56    0.00    3.28     0.0     0.0
# node B    0.00     0.0    1.13    0.64    0.00     0.0     0.0
# node F    0.00     0.5    0.00    0.00    0.00     0.0     0.0
# node J    0.00     0.0    0.00    0.00    0.00     0.0     0.0
# node M    0.00     0.0    0.00    0.00    0.00     0.0     0.0
# node C    0.00     0.0    0.00    0.00    0.50     0.0     0.0
# node Z    0.45     0.0    0.00    0.00    0.00     0.0     0.0

Now you can make the interactive network with various parameters:现在您可以使用各种参数制作交互式网络:

# Example A: simple interactive network
out = d3graph(adjmat)

# Example B: Color nodes
out = d3graph(adjmat, node_color=adjmat.columns.values)

# Example C: include node size
node_size = [10,20,10,10,15,10,5]
out = d3graph(adjmat, node_color=adjmat.columns.values, node_size=node_size)

# Example D: include node-edge-size
out = d3graph(adjmat, node_color=adjmat.columns.values, node_size=node_size, node_size_edge=node_size[::-1], cmap='Set2')

# Example E: include node-edge color
out = d3graph(adjmat, node_color=adjmat.columns.values, node_size=node_size, node_size_edge=node_size[::-1], node_color_edge='#00FFFF')

# Example F: Change colormap
out = d3graph(adjmat, node_color=adjmat.columns.values, node_size=node_size, node_size_edge=node_size[::-1], node_color_edge='#00FFFF', cmap='Set2')

# Example H: Include directed links. Arrows are set from source -> target
out = d3graph(adjmat, node_color=adjmat.columns.values, node_size=node_size, node_size_edge=node_size[::-1], node_color_edge='#00FFFF', cmap='Set2', directed=True)

Below are screenshots of the results.下面是结果的截图。 The changes may be very subtle.这些变化可能非常微妙。 An interactive plot can be found here and the github here .可以在此处找到交互式绘图,在此处找到 github。

d3graph 示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM