简体   繁体   English

Networkx viz - 如何在以圆形布局绘制节点之前对节点进行排序?

[英]Networkx viz - How do I sort nodes before plotting them in circular layout?

I am trying to create timetables or cardioid graph using network in python this is my code我正在尝试使用 python 中的网络创建时间表或心形图这是我的代码

import matplotlib.pyplot as plt
import networkx as nx
G = nx.Graph()
n = 10
for i in range(1,n):
    if i*2 < n:
        G.add_node(i, weight=i)
        G.add_node(i*2, weight=i*2)
        G.add_edge(i, i*2)
    else:
        G.add_node(i, weight=i)
        G.add_node(i*2-n, weight=i*2-n)
        G.add_edge(i,i*2 - n)
plt.figure(figsize=(10,6))
nx.draw_networkx(G, pos=nx.circular_layout(G), node_size=1000)

But then I am getting something like this enter image description here Whereas I want nodes to be in aa sorted manner like 0,1,2... in circular format, how do I achieve that?但是后来我得到了类似的内容,在此处输入图像描述而我希望节点以循环格式的 0、1、2 等排序方式出现,我该如何实现?

You can achieve it by using您可以通过使用来实现它

nx.draw_networkx(G, pos=nx.circular_layout(sorted(G.nodes()), node_size=1000)

to get the nodes sorted in an anticlockwise manner.以逆时针方式对节点进行排序。
Otherwise, for a clockwise ordering you can use否则,对于顺时针顺序,您可以使用

nx.draw_networkx(G, pos=nx.circular_layout(sorted(G.nodes(), reverse=True)), node_size=1000)

Example:例子:

在此处输入图像描述

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

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