繁体   English   中英

在 Networkx 中制作圆形图而不是线图

[英]Make circular Graph instead of the line Graph in Networkx

可以说我有以下情况:

import matplotlib.pyplot as plt
import networkx as nx
import pandas as pd 

d = {'label': [1, 2, 3, 4, 5], 'size': [10, 8, 6, 4, 2], 'dist': [0, 2, 4, 6, 8]}
df = pd.DataFrame(data=d)
df 
G = nx.Graph()
for _, row in df.iterrows():
    G.add_node(row['label'], pos=(row['dist'], 0), size=row['size'])
biggest_node = 1
for node in G.nodes:
    if node != biggest_node:
        G.add_edge(biggest_node, node)

nx.draw(G,
        pos={node: attrs['pos'] for node, attrs in G.nodes.items()},
        node_size=[node['size'] * 100 for node in G.nodes.values()],
        with_labels=True
        )
plt.show()

这个例子来自我之前关于同一问题的问题的答案

我现在想要做的是获得一个类似的图表,但我希望节点在圆周上对齐而不是直线。 rest保持不变,有什么办法吗? 很明显,一个人可以得到那个。 离原点最远的点将在相反的位置。

因为,我知道你只有一个“原点”(dist 为 0 的节点),所以你可以使用pos=spring_layout(G)因为它使用 fruchterman-Reingold 算法来绘制图形,当有只有一个父节点。

nx.draw(G,
    pos=nx.spring_layout(G),
    node_size=[node['size'] * 100 for node in G.nodes.values()],
    with_labels=True
    )

在此处输入图像描述

暂无
暂无

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

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