3

I am completely new to graphs. I have a 213 X 213 distance matrix. I have been trying to visualize the distance matrix using network and my idea is that far apart nodes will appear as separate clusters when the graph will be plotted. So I am creating a graph with nodes representing column index. I need to keep track of nodes in order to label it afterwards. I need to add edges in certain order so I need to keep track of nodes and their labels.

Here is the code:

import networkx as nx
G = nx.Graph()
G.add_nodes_from(time_pres) ##time_pres is the list of labels that I want specific node to have

 for i in range(212):
    for j in range(i+1, 212):
        color = ['green' if j == i+1 else 'red'][0]
        edges.append((i,j, dist[i,j], 'green')) ##This thing requires allocation of distance as per the order in dist matrirx
        G.add_edge(i,j, dist = dist[i,j], color = 'green')

The way I am doing right now, it is allocating nodes with id as a number which is not as per the index of labels in time_pres.

7
  • It's not clear what you're asking. Is your way not working, or are you asking whether there is a better way? Where are the variables color and edges useful in this code? Could you give a small example with a 2x2 or 3x3 distance matrix for us to visualise what you're trying to achieve? Commented Jul 16, 2014 at 0:44
  • In 3X3, each column will represent one event in time. So I have events 1->2->3. I want to show this time evolution by way of coloring the edges joining adjacent nodes with the same color. I also have distances between nodes which I am passing through $dist$ attribute. Way I have written the code, I am not able to specify node number along with label. So node number '1' gets assigned a label which might be some index other than 1 of time_pres. So I am not able to keep account of which node gets assigned to which label. Commented Jul 16, 2014 at 1:12
  • What values does the time_pres variable hold? Just give me 9, for a 3x3 example. Commented Jul 16, 2014 at 1:25
  • time_pres =[('person1', '1878'), ('person2','1879'), ...] and dist is a numpy matrix of dimension 213 X 213. Commented Jul 16, 2014 at 1:58
  • Tell me if I understand this properly. If you have two nodes with labels ('person1', '1878') and ('person2', '1879'), and indices 0 and 1 respectively, then time_pres = [('person1', '1878'), ('person2', '1879')]. And dist[0,1] is the distance between between ('person1', '1878') and ('person2', '1879'). Correct? Commented Jul 16, 2014 at 8:08

1 Answer 1

4

I can answer the question you seem to be asking, but this won't be the end of your troubles. Specifically, I'll show you where you go wrong.

So, we assume that the variable time_pres is defined as follows

time_pres = [('person1', '1878'), ('person2', '1879'), etc)]

Then,

G.add_nodes_from(time_pres)

Creates the nodes with labels ('person1', '1878'), ('person2', '1879'), etc. These nodes are held in a dictionary, with keys the label of the nodes and values any additional attributes related to each node. In your case, you have no attributes. You can also see this from the manual online, or if you type help(G.add_nodes_from).

You can even see the label of the nodes by typing either of the following lines.

G.nodes()        # either this
G.node.keys()    # or this

This will print a list of the labels, but since they come from a dictionary, they may not be in the same order as time_pres. You can refer to the nodes by their labels. They don't have any additional id numbers, or anything else.

Now, for adding an edge. The manual says that any of the two nodes will be added if they are not already in the graph. So, when you do

G.add_edge(i, j, dist = dist[i,j], color = 'green')

where, i and j are numbers, they are added in the graph since they don't already exist in the graph labels. So, you end up adding the nodes i and j and the edge between them. Instead, you want to do

G.add_edge(time_pres[i], time_pres[j], dist = dist[i,j], color = 'green')

This will add an edge between the nodes time_pres[i] and time_pres[j]. As far as I understand, this is your aim.


However, you seem to expect that when you draw the graph, the distance between nodes time_pres[i] and time_pres[j] will be decided by the attribute dist=dist[i,j] in G.add_edge(). In fact, the position of a node is decided by tuple holding the x and y positions of the node. From the manual for nx.draw().

pos : dictionary, optional

A dictionary with nodes as keys and positions as values. If not specified a spring layout positioning will be computed. See networkx.layout for functions that compute node positions.

If you don't define the node positions, they will be generated randomly. In your case, you would need a dictionary like

pos = {('person1', '1878'): (23, 10),
       ('person2', '1879'): (18, 11),
       etc}

Then, the coordinates between the nodes i and j would result to a distance equal to dist[i,j]. You would have to figure out these coordinates, but since you haven't made it clear exactly how you derived the matrix dist, I can't say anything about it.

Sign up to request clarification or add additional context in comments.

3 Comments

Hey, Thanks a lot for your explanation. It seems to be clear to me now. Though I would love to know if there is an algorithm that can give me coordinates of nodes automatically given the distnace matrix. Explanation of dist goes like this: if I have a 3X2 matrix with each column represents a node of 2D vector, I can create a distance matrix of 3X3 dimension(which will be symmetric).
Unfortunately, this isn't clear enough. It also deserves its own question, either on Stack Overflow, or Math Overflow. With 200+ nodes this can get very complex.
I am sorry, 3X2 matrix should be 2X3 in my comment above. Somebody told me to use Gephi for graph visualization so I am learning Gephi now. I hope I can get a solution through that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.