簡體   English   中英

Networkx:使用位置在節點上方提升標簽

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM