简体   繁体   English

如何在networkx的图形中的节点上添加标签?

[英]How to add labels to nodes in a graph in networkx?

I am writing a code for integer partition and constructing a graph where each node is a partition. 我正在为整数分区编写代码,并构造一个其中每个节点都是分区的图。 I would like to label the nodes in the graph with partition elements like {2,1,1}, {1,1,1,1},{2,2} etc. 我想用诸如{2,1,1},{1,1,1,1},{2,2}等分区元素来标记图中的节点。

So I want to know, how to label a node in networkx. 所以我想知道如何在networkx中标记节点。

I have already seen the code for labeling node but I didn't get it. 我已经看过用于标记节点的代码,但没有得到。 The code is given below: 代码如下:

nx.draw_networkx_nodes(G,pos,
                       nodelist=[0,1,2,3],
                       node_color='r',
                       node_size=500,
                    alpha=0.8)

But what I want is to label individual nodes of already constructed graph! 但是我想要标记已经构造的图形的各个节点!

I am providing my code here, so that you can have better understanding of it. 我在这里提供我的代码,以便您可以更好地理解它。

import networkx as nx
from matplotlib import pylab as pl

def partitions(num):
    final=[[num]]

    for i in range(1,num):
        a=num-i
        res=partitions(i)
        for ele in res:
            if ele[0]<=a:
                final.append([a]+ele)

    return final

def drawgraph(parlist):
    #This is to draw the graph
    G=nx.Graph()
    length=len(parlist)
    print "length is %s\n" % length

    node_list=[]

    for i in range(1,length+1):
        node_list.append(i)

    G.add_cycle(node_list)

    nx.draw_circular(G)
    pl.show()

Please help me. 请帮我。

Thank you very much 非常感谢你

Since your node_list was composed of integers, your nodes got the string representation of those integers as labels. 由于您的node_list由整数组成,因此您的节点将这些整数的字符串表示形式作为标签。 But your nodes can be any hashable object, not just integers. 但是您的节点可以是任何可哈希对象,而不仅仅是整数。 So the simplest thing to do would be to make your node_list the string representation of the items in parlist . 因此,最简单的方法是使node_list成为parlist各项的字符串表示parlist (The items in parlist are lists, which are mutable and therefore not hashable. That's why we can't just use parlist as our node_list .) parlist中的项目是列表,它们是可变的,因此不可散列。这就是为什么我们不能仅将parlist用作node_list 。)

There is also the function nx.relabel_nodes , which we could use instead, but I think just giving the nodes the right label in the first place is simpler. 还有一个函数nx.relabel_nodes ,我们可以改用它,但我认为首先为节点提供正确的标签会更简单。

import networkx as nx
import matplotlib.pyplot as plt


def partitions(num):
    final = [[num]]

    for i in range(1, num):
        a = num - i
        res = partitions(i)
        for ele in res:
            if ele[0] <= a:
                final.append([a] + ele)

    return final


def drawgraph(parlist):
    G = nx.Graph()
    length = len(parlist)
    print "length is %s\n" % length
    node_list = [str(item) for item in parlist]
    G.add_cycle(node_list)
    pos = nx.circular_layout(G)
    draw_lifted(G, pos)


def draw_lifted(G, pos=None, offset=0.07, fontsize=16):
    """Draw with lifted labels
    http://networkx.lanl.gov/examples/advanced/heavy_metal_umlaut.html
    """
    pos = nx.spring_layout(G) if pos is None else pos
    nx.draw(G, pos, font_size=fontsize, with_labels=False)
    for p in pos:  # raise text positions
        pos[p][1] += offset
    nx.draw_networkx_labels(G, pos)
    plt.show()

drawgraph(partitions(4))

yields 产量

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM