epo*_*po3 7 python visualization network-analysis pymnet
我想使用pymnet可视化多层网络。包文档中的示例显示了如何绘制多层网络(下图的左侧),但我想添加另一个层(橙色),该层将显示在与蓝色层相似的级别。我知道如何添加另一层,但它会在蓝色层之上。我需要的是我当前情节旁边的一个图层。
可以使用以下方法创建原始图:
from pymnet import *
fig=draw(er(10,3*[0.4]),layout="spring")
Run Code Online (Sandbox Code Playgroud)
这就是我想要得到的:
有没有办法在pymnet 中实现它?如果没有,是否有另一个包可以绘制此图?
这是一个仅使用networkx创建多层图并自行计算节点位置的解决方案。
为了便于解释,我们随机创建了一个包含 30 个节点的小图。
这些图层是具有附加属性的子图:( x , y ) 坐标和颜色。坐标用于在 2D 网格中相对定位图层。
import networkx as nx
# create a random graph of 30 nodes
graph = nx.fast_gnp_random_graph(30, .2, seed=2019)
# Layers have coordinates and colors
layers = [
(nx.Graph(), (0, 0), "#ffaaaa"),
(nx.Graph(), (0, 1), "#aaffaa"),
(nx.Graph(), (0, 2), "#aaaaff"),
(nx.Graph(), (1, 2), "#ffa500"),
]
Run Code Online (Sandbox Code Playgroud)
每层都填充有主图的节点。在这里,我们决定将节点列表拆分为不同的范围(图中节点的开始和结束索引)。
每个节点的颜色存储在color_map中。该变量稍后在图形绘制过程中使用。
import itertools
# node ranges in the graph
ranges = [(0, 6), (6, 15), (15, 20), (20, 30)]
# fill the layers with nodes from the graph
# prepare the color map
color_map = []
for (layer, coord, color), (start, end) in zip(layers, ranges):
layer.add_nodes_from(itertools.islice(graph.nodes, start, end))
color_map.extend([color for _ in range(start, end)])
Run Code Online (Sandbox Code Playgroud)
然后,我们就可以计算出每个节点的位置。节点位置根据图层坐标进行移动。
# Calculate and move the nodes position
all_pos = {}
for layer, (sx, sy), color in layers:
pos = nx.circular_layout(layer, scale=2) # or spring_layout...
for node in pos:
all_pos[node] = pos[node]
all_pos[node] += (10 * sx, 10 * sy)
Run Code Online (Sandbox Code Playgroud)
我们现在可以绘制图表:
import matplotlib.pyplot as plt
# Draw and display the graph
nx.draw(graph, all_pos, node_size=500, node_color=color_map, with_labels=True)
plt.show()
Run Code Online (Sandbox Code Playgroud)
结果如下:
当然,您可以使用 3D 网格并使用投影来获得类似 3D 的预览。
| 归档时间: |
|
| 查看次数: |
995 次 |
| 最近记录: |