[英]Networkx: Raising the labels above the node using position
我对 networkx 很陌生(今天才开始!):我正在使用这两个链接并复制:
所以我的看起来像这样:
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 教程,那就更好了!
您首先需要绘制图形,然后添加值(或为标签的位置创建第二个变量)。 如果你再次阅读定位标签的代码,你会看到他们首先绘制图形,然后修改布局并绘制标签。
您的代码只是移动所有内容,即沿 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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.