【问题标题】:Networkx: Raising the labels above the node using positionNetworkx:使用位置提升节点上方的标签
【发布时间】:2020-03-03 07:16:23
【问题描述】:

我对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】:

    您首先需要绘制图形,然后添加值(或为标签的位置创建第二个变量)。 如果您再次阅读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")
    

    【讨论】:

    • 谢谢!它解决了这个问题 :) 在一个侧节点上,我现在希望使它具有交互性,因此使用 Bokeh 切换到 networkx。你熟悉吗?你能推荐一些好的教程吗?非常感谢!
    猜你喜欢
    • 2017-10-09
    • 1970-01-01
    • 2023-01-02
    • 1970-01-01
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    • 2019-05-06
    • 1970-01-01
    相关资源
    最近更新 更多