Python NetworkX - Why are graphs always randomly rotated?

后端 未结 3 1877
旧巷少年郎
旧巷少年郎 2021-01-13 07:44

If I generate the same graph multiple times using NetworkX and Matplotlib it\'s rotated randomly on every generation:

Run 1:

Run 2:

Without changin

3条回答
  •  鱼传尺愫
    2021-01-13 08:32

    The algorithm has a random initialization - this is quite standard for network layouts. You will have the same issue with any graph library. You can replicate the same layout in NetworkX in two ways: save the node position dictionary (for example as a json) and reload it every time you want to visualize, or pass a seed to the layout algorithm:

    seed (int, RandomState instance or None optional (default=None)) – Set the random state for deterministic node layouts. If int, seed is the seed used by the random number generator, if numpy.random.RandomState instance, seed is the random number generator, if None, the random number generator is the RandomState instance used by numpy.random.

    hope that helps!

提交回复
热议问题