Networkx:使用位置将标签升高到节点上方

问题描述 投票:0回答:1

我对networkx还是陌生的(今天才刚开始!):我正在使用这两个链接并进行复制:

This is for creating the network

This is for how I tried adjusting the label positions

所以我看起来像这样:

layout = nx.spring_layout(g,k=0.2,iterations=50)
for l in layout:  # raise text positions
    layout[l][1] += 0.5

我将0.5调整为较小甚至更大的值,但是没有任何反应,也没有调整。完全没有变化。我在做什么错?

这两个代码组合起来如下:

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

plt.figure(figsize=(12, 12))

df = pd.read_csv(df_path)

# 1. Create the graph
g = nx.from_pandas_edgelist(df, source='name', target='club') 
# 2. Create a layout for our nodes 
layout = nx.spring_layout(g,k=0.2,iterations=50)
for l in layout:  # raise text positions
    layout[l][1] += 0.5
# 3. Draw the parts we want
nx.draw_networkx_edges(g, layout, edge_color='#AAAAAA')

clubs = [node for node in g.nodes() if node in df.club.unique()]
size = [g.degree(node) * 80 for node in g.nodes() if node in df.club.unique()]
nx.draw_networkx_nodes(g, layout, nodelist=clubs, node_size=size, node_color='lightblue')

people = [node for node in g.nodes() if node in df.name.unique()]
nx.draw_networkx_nodes(g, layout, nodelist=people, node_size=100, node_color='#AAAAAA')

high_degree_people = [node for node in g.nodes() if node in df.name.unique() and g.degree(node) > 1]
nx.draw_networkx_nodes(g, layout, nodelist=high_degree_people, node_size=100, node_color='#fc8d62')

club_dict = dict(zip(clubs, clubs))
nx.draw_networkx_labels(g, layout, labels=club_dict)

# 4. Turn off the axis because I know you don't want it
plt.axis('off')

plt.title("Revolutionary Clubs")

非常感谢您!

另外,有没有人在networkx上有出色的教程?我一直在谷歌搜索,但没有发现太多。而且,如果您知道networkx教程,它们显示了如何构建交互式网络,那就更好了!

python graph label networkx sna
1个回答
0
投票

您首先需要绘制图形,然后添加值(或为标签的位置创建第二个变量)。如果再次阅读code for positioning the labels,将会看到他们首先绘制图形,然后修改布局并绘制标签。

© www.soinside.com 2019 - 2024. All rights reserved.