在 NetworkX 中重现相同的图形

Reproduce same graph in NetworkX

我想改进我的图表。 存在问题如下:

  1. 如何创建一致的 graph.the 图形本身并不一致,每次我执行/ 运行 代码时,它会生成不同的图像。 url.
  2. 中显示了不一致的图形

  1. 如何自定义整张图/图片的大小并使其变大
  2. 如何为对象设置永久位置'a'以便它始终出现在第一个/顶部位置
  3. 如何为每个关系自定义箭头长度。

如果有人可以提供一些注释或建议,我们将不胜感激

这是我的代码:

Unique_liss= ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm']

edgesList= [('a', 'b'), ('b', 'c '), ('c ', 'd'), ('d', 'e'), ('d', 'f'), ('e', 'g'), ('f', 'g'), ('g', 'h'), ('h', 'i '), ('i ', 'j'), ('j', 'k'), ('j', 'l'), ('k', 'm'), ('l', 'm')]

import networkx as nx
g = nx.DiGraph()
g.add_nodes_from(Unique_liss)
g.add_edges_from(edgesList)
nx.to_pandas_adjacency(g)

G = nx.DiGraph()
for node in edgesList:
    G.add_edge(*node,sep=',')

A = nx.adjacency_matrix(G).A

nx.draw(G, with_labels=True, node_size = 2000,
        node_color = 'skyblue')

为了获得确定性的节点布局,您可以对上图使用 NetworkX 的 layouts, which allow you to specify a seed. Here's an example using nx.spring_layout 之一:

from matplotlib import pyplot as plt

seed = 31
pos = nx.spring_layout(G, seed=seed)
plt.figure(figsize=(10,6))
nx.draw(G, pos=pos, with_labels=True, node_size = 1500,
        seed=seed, node_color = 'skyblue')

如果您重新运行以上内容,您将获得完全相同的布局。

要自定义图形大小,您有多种选择。最简单的baing 设置图形大小如上plt.figure(figsize=(x,y))。您还可以使用 nx.spring_layout.

中的 scale 参数控制图中图形的大小

根据最后一点,您似乎无法为每条边设置特定的箭头大小。从 [docs](arrowsize : int, optional (default=10)) 你所拥有的是:

arrowsize : int, optional (default=10)

因此您只能将此值设置为 int,这将导致所有边箭头的大小相等。

对于无法阅读答案中的评论的任何人。我发现正如@amj 在组件中提到的那样,除了上面@yatu 的回复之外,您还需要在 numpy.

中设置种子
import numpy as np 
seed = 31 
np.random.seed(seed)