Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node labels using networkx

I'm creating a graph out of given sequence of Y values held by curveSeq. (the X values are enumerated automatically: 0,1,2...)

i.e for curveSeq = [10,20,30], my graph will contain the points:

<0,10>, <1,20>, <2,30>.

I'm drawing a series of graphs on the same nx.Graph in order to present everything in one picture.

My problem is:

  • Each node presents its location. i.e the node in location <0,10> presents its respective label and I don't know how to remove it.
  • There are specific nodes that I want to add a label to, but I don't know how.

for example, for the sequence:

[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1,1]

The received graph is:

graph

The code is:

for point in curveSeq:
                cur_point = point
                #assert len(cur_point) == 2
                if prev_point is not None:
                    # Calculate the distance between the nodes with the Pythagorean
                    # theorem
                    b = cur_point[1] - prev_point[1]
                    c = cur_point[0] - prev_point[0]
                    a = math.sqrt(b ** 2 + c ** 2)
                    G.add_edge(cur_point, prev_point, weight=a)
                G.add_node(cur_point)
                pos[cur_point] = cur_point
                prev_point = cur_point
            #key:
            G.add_node((curve+1,-1))
            pos[(curve+1,-1)] = (curve+1,-1)

            nx.draw(G, pos=pos, node_color = colors[curve],node_size=80)
            nx.draw_networkx_edges(G,pos=pos,alpha=0.5,width=8,edge_color=colors[curve])

     plt.savefig(currIteration+'.png')
like image 579
Maoritzio Avatar asked Mar 21 '13 13:03

Maoritzio


People also ask

How many nodes can NetworkX handle?

For NetworkX, a graph with more than 100K nodes may be too large. I'll demonstrate that it can handle a network with 187K nodes in this post, but the centrality calculations were prolonged. Luckily, there are some other packages available to help us with even larger graphs.

Which data type can be used as the content of a node in NetworkX?

In NetworkX, nodes can be any hashable object e.g., a text string, an image, an XML object, another Graph, a customized node object, etc. Python's None object is not allowed to be used as a node.

What is a DiGraph NetworkX?

A DiGraph stores nodes and edges with optional data, or attributes. DiGraphs hold directed edges. Self loops are allowed but multiple (parallel) edges are not. Nodes can be arbitrary (hashable) Python objects with optional key/value attributes.


1 Answers

You can add the with_labels=False keyword to suppress drawing of the labels with networkx.draw(), e.g.

networkx.draw(G, pos=pos, node_color=colors[curve],
    node_size=80, with_labels=False)

Then draw specific labels with

networkx.draw_networkx_labels(G,pos, labels)

where labels is a dictionary mapping node ids to labels.

Take a look at this example: https://networkx.org/documentation/stable/auto_examples/drawing/plot_labels_and_colors.html

like image 103
Aric Avatar answered Sep 17 '22 13:09

Aric