在pymnet中修改多层网络图的绘制

7
我想使用pymnet可视化一个多层网络。包的documentation中的示例展示了如何绘制多层网络(下图左侧),但我想添加另一层(橙色),它将显示在与蓝色层类似的级别上。我知道如何添加另一层,但它会位于蓝色层之上。我需要的是一个紧挨着我的当前图的层。
可以使用以下代码创建原始图:
from pymnet import *
fig=draw(er(10,3*[0.4]),layout="spring")

这就是我想要得到的:

enter image description here

有没有办法在pymnet中实现这个?如果没有,是否有其他软件包可以绘制此图?


pymnet 库在 PyPi 上不可用。如何安装它?哪个仓库? - Laurent LAPORTE
1
源代码可在BitBucket中获取。代码质量需要提高...缺少软件包依赖项...存在许多不便之处...在我看来,这个库更像是一个概念验证而不是一个生产就绪的库。考虑使用其他东西。 - Laurent LAPORTE
2
根据源代码(pymnet/visuals/drawnet.py),一个图形只是一堆垂直层叠在一起。你不能将两个堆叠在一起并排放置。 - Laurent LAPORTE
1
谢谢。你知道还有哪个包可以绘制这个图吗? - epo3
1个回答

4
这里提供了一种解决方案,只需使用networkx创建多层图,并通过自己的方法计算节点位置。
为了说明该解决方案,我们创建一个包含30个随机节点的小图。
这些层是带有其他属性的子图:(xy)坐标和颜色。这些坐标用于相对定位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"),
]

每个层都填充有主图的节点。 在这里,我们决定将节点列表分成不同的范围(图中节点的起始和结束索引)。

每个节点的颜色存储在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)])

然后,我们可以计算每个节点的位置。 根据层坐标,节点位置会发生位移。
# 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)

我们现在可以绘制图表:
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()

结果如下图所示:

Graph with layers

当然,你可以使用三维网格,并使用投影来获得类似于三维的预览。

嗨@Laurent LAPORTE。我看到了这个问题和你的答案。我在pymnet上发布了一个问题。我不知道你是否知道答案并能帮助我,但如果你能看一下,我会很感激。非常感谢:https://stackoverflow.com/questions/66947554/how-to-change-color-of-nodes-in-a-network - Math

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接