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

Networkx: Raising the labels above the node using position

我是 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 教程,那就更好了!

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

您的代码只是简单地移动所有内容,即沿 y 轴移动标签和边缘。 我已经更正了您代码中调整的位置:

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)
#
# ----- removed correction
#

# 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))

# ------> and move it here
for l in layout:  # raise text positions
    layout[l][1] += 0.1  # probably small value enough
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")