33

I imported my Facebook data onto my computer in the form of a .json file. The data is in the format:

{
  "nodes": [
    {"name": "Alan"},
    {"name": "Bob"}
  ],
  "links": [
    {"source": 0, "target: 1"}
  ]
}

Then, I use this function:

def parse_graph(filename):
    """
    Returns networkx graph object of facebook
    social network in json format
    """
    G = nx.Graph()
    json_data = open(filename)
    data = json.load(json_data)
    # The nodes represent the names of the respective people
    # See networkx documentation for information on add_* functions
    nodes = data["nodes"]
    G.add_nodes_from([n["name"] for n in nodes])
    G.add_edges_from(
        [
            nodes[e["source"]]["name"],
            nodes[e["target"]]["name"]) for e in data["links"]
        ]
    )
    json_data.close()
    return G

to enable this .json file to be used a graph on NetworkX. If I find the degree of the nodes, the only method I know how to use is:

degree = nx.degree(p)

Where p is the graph of all my friends. Now, I want to plot the graph such that the size of the node is the same as the degree of that node. How do I do this?

Using:

nx.draw(G, node_size=degree)

didn't work and I can't think of another method.

3 Answers 3

55

Update for those using networkx 2.x

The API has changed from v1.x to v2.x. networkx.degree no longer returns a dict but a DegreeView Object as per the documentation.

There is a guide for migrating from 1.x to 2.x here.

In this case it basically boils down to using dict(g.degree) instead of d = nx.degree(g).

The updated code looks like this:

import networkx as nx
import matplotlib.pyplot as plt

g = nx.Graph()
g.add_edges_from([(1,2), (2,3), (2,4), (3,4)])

d = dict(g.degree)

nx.draw(g, nodelist=d.keys(), node_size=[v * 100 for v in d.values()])
plt.show()

nx.degree(p) returns a dict while the node_size keywod argument needs a scalar or an array of sizes. You can use the dict nx.degree returns like this:

import networkx as nx
import matplotlib.pyplot as plt

g = nx.Graph()
g.add_edges_from([(1,2), (2,3), (2,4), (3,4)])

d = nx.degree(g)

nx.draw(g, nodelist=d.keys(), node_size=[v * 100 for v in d.values()])
plt.show()

enter image description here

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

2 Comments

Great answer! As I mention below, if the data has zero degree nodes that a user will want to show, the node size should have some value added to it, like: [(v+1) * 100 for v in d.values()], so that the zero degree nodes show up.
First code piece prints without any string inside the node @miles82
4

@miles82 provided a great answer. However, if you've already added the nodes to your graph using something like G.add_nodes_from(nodes), then I found that d = nx.degree(G) may not return the degrees in the same order as your nodes.

Building off the previous answer, you can modify the solution slightly to ensure the degrees are in the correct order:

d = nx.degree(G)
d = [(d[node]+1) * 20 for node in G.nodes()]

Note the d[node]+1, which will be sure that nodes of degree zero are added to the chart.

3 Comments

Haven't you checked if the G.nodes() always returns nodes in one order? In this case it the final code will look like degrees = nx.degree(G) nx.draw(g, nodelist=G.nodes(), node_size=[(degrees[v] + 1) * 100 for v in G.nodes()]) `
This is great, and should be the accepted answer!
This answer worked for me. @miles82 answer gave me 'dict_keys' object has no attribute 'index' error. Using d = [(d[node]+1) * 100 for node in G.nodes()] produced identical chart to @miles82 answer.
4

other method if you still get 'DiDegreeView' object has no attribute 'keys'

1)you can first get the degree of each node as a list of tuples

2)build a node list from the first value of tuple and degree list from the second value of tuple.

3)finally draw the network with the node list you've created and degree list you've created

here's the code:

    list_degree=list(G.degree()) #this will return a list of tuples each tuple is(node,deg)
    nodes , degree = map(list, zip(*list_degree)) #build a node list and corresponding degree list
    plt.figure(figsize=(20,10))
    nx.draw(G, nodelist=nodes, node_size=[(v * 5)+1 for v in degree])
    plt.show() #ploting the graph 

Comments

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.