【问题标题】:How to add node attributes next to node name in Python networkx?如何在 Python networkx 中的节点名称旁边添加节点属性?
【发布时间】:2018-08-15 19:13:33
【问题描述】:

我想绘制一个连接同事和电影偏好的二分图。如何在图中同时显示节点名称(电影名称或同事名称)和节点属性(无论是同事还是电影)?

我当前的代码只显示节点属性并删除节点名称。

我的代码:

BG = nx.Graph()
BG.add_nodes_from(employees, bipartite=0, _type='employee')
BG.add_nodes_from(movies, bipartite=1, _type='movie')
edgeinfo = pd.read_csv('Employee_Movie_Choices.txt', sep='\t')
edges = [tuple(x) for x in edgeinfo.values]
BG.add_edges_from(edges)

labels = dict((n,d['_type']) for n,d in BG.nodes(data=True))

%matplotlib notebook
import matplotlib.pyplot as plt

plt.figure()
pos = nx.spring_layout(BG)
edges = BG.edges()
nx.draw_networkx(BG, pos, edges=edges, labels=labels)

如果我创建标签元组,他会给我一个错误:

BG = nx.Graph()
BG.add_nodes_from(employees, bipartite=0, _type='employee')
BG.add_nodes_from(movies, bipartite=1, _type='movie')
edgeinfo = pd.read_csv('Employee_Movie_Choices.txt', sep='\t')
edges = [tuple(x) for x in edgeinfo.values]
BG.add_edges_from(edges)

labels = dict((n,d['_type']) for n,d in BG.nodes(data=True))  ###ik krijg hier naam movie en employee niet meer bij !!!
labels_new = [(k, v) for k, v in labels.items()]
#labels = [tuple(n,d['_type']) for n, d in BG.nodes(data=True)]
#nx.draw(BG, labels=labels)

%matplotlib notebook
import matplotlib.pyplot as plt

plt.figure()
pos = nx.spring_layout(BG)
edges = BG.edges()
nx.draw_networkx(BG, pos, edges=edges, labels=labels_new)

错误: ---> nx.draw_networkx(BG,pos,edges=edges,labels=labels_new) AttributeError: 'list' 对象没有属性 'items'

【问题讨论】:

    标签: python networkx labels


    【解决方案1】:

    为什么会出现错误

    来自draw_networkx的文档 , labels 需要是字典,而 labels_new 你输入的是列表。

    labels (dictionary, optional (default=None)) – 节点标签 以文本标签节点为键的字典

    根据您的代码,

    labels_new=[(k, v) for k, v in labels.items()]

    因此,出现错误AttributeError: 'list' object has no attribute 'items'

    解决方法:自定义labels 字典

    我没有数据,但可以快速破解

    labels = dict((n, "(" + n + "," + d['_type'] + ")") for n,d in BG.nodes(data=True))
    

    演示

    import networkx as nx
    from networkx.algorithms import bipartite
    %matplotlib notebook
    import matplotlib.pyplot as plt
    
    BG = nx.Graph()
    employees = [str(i) for i in range(3)]
    movies = ["mA", "mB", "mC"]
    BG.add_nodes_from(employees, bipartite=0, _type='emp')
    BG.add_nodes_from(movies, bipartite=1, _type='mov')
    edges = [("0", "mA"), ("0", "mC"), ("1", "mA"),("1", "mB"), ("2", "mA")]
    BG.add_edges_from(edges)
    labels = dict((n, "(" + n + "," + d['_type'] + ")") for n,d in BG.nodes(data=True))
    
    # Setting up pos for drawing bipartite graph. See the reference for more info
    X, Y = bipartite.sets(BG)
    pos = dict()
    pos.update( (n, (1, i)) for i, n in enumerate(X) ) # put nodes from X at x=1
    pos.update( (n, (2, i)) for i, n in enumerate(Y) ) # put nodes from Y at x=2
    
    plt.figure()
    edges = BG.edges()
    nx.draw_networkx(BG, pos, edges=edges, labels=labels)
    

    参考

    mdml's answer for drawing bipartite graph

    【讨论】:

    • 这行得通!非常感谢您帮助我!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-17
    • 1970-01-01
    • 2013-12-21
    • 2019-03-28
    • 2020-02-06
    • 1970-01-01
    相关资源
    最近更新 更多