How to add nodes and edges to a network analysis graph in Networkx?

Sam

I am trying to learn network analysis, so I am using Hillary Clinton’s emails online to see who emailed who.

My data is in a dictionary called hrc_dict. I have a tuple of the sender and receiver followed by the frequency of the emails. This is part of the dictionary:

{('Hillary Clinton', 'Cheryl Mills'): 354, ('Hillary Clinton', 'l'): 1, ('Linda Dewan', 'Hillary Clinton'): 1, ('Hillary Clinton', 'Capricia Marshall'): 9, ('Phillip Crowley', 'Hillary Clinton'): 2, ('Cheryl Mills', 'Anne-Marie Slaughter'): 1}

I am using Networkx in Jupyter to create a graph. My code is below:

import networkx as nx
import matplotlib.pyplot as plt

G = nx.Graph()

G.add_nodes_from(hrc_dict)

for s, r in hrc_dict:
    G.add_edge((s,r), hrc_dict[(s,r)])

G.add_edge((s,r), hrc_dict[(s,r)])

When I call nx.Graph(), nothing prints out and when I call G.nodes(), not all the nodes are showing up. I have pasted some of the output here:

[1, 2, 3, 4, 5, 6, 7, 8, 'Mark Penn', 10, ('Todd Stern', 'Hillary Clinton'), 12,]

When I call G.edges(), I get the below, which seems right

[(1, ('Hillary Clinton', 'l')), (1, ('Linda Dewan', 'Hillary Clinton')), (1, ('Hillary Clinton', 'Thomas Shannon')), (1, ('Cheryl Mills', 'Anne-Marie Slaughter')), (1, ('Christopher Butzgy', 'Hillary Clinton’))]

Does anyone know how I can add nodes correctly to my graph. I assume that each person needs to be a node, so how do I break up the tuple and add the names separately? Are the edges showing correctly or do I need to enter them differently?

Bonlenfum

To add each person as a node, you also need to change the use of add_nodes_from.

Something like this:

srcs, dests = zip(* [(fr, to) for (fr, to) in hrc_dict.keys()])
G.add_nodes_from(srcs+dests)

now means that the list of nodes from G.nodes() will be:

['Cheryl Mills',
 'Capricia Marshall',
 'Anne-Marie Slaughter',
 'Phillip Crowley',
 'Hillary Clinton',
 'l',
 'Linda Dewan']

(you don't get any duplicates because networkx stores graphs as a dictionary).

Note: if you use the method below for adding the edges, there isn't any need to add the nodes first -- but in case there is some reason why you might have nodes that have no neighbours (or another reason why nodes only is important), this code will do it.

Then add the edges basically as per Joel's answer; but also note the use of the attribute "weight", so the layout can make use of information directly.

import networkx as nx
import matplotlib.pyplot as plt

hrc_dict = {('Hillary Clinton', 'Cheryl Mills'): 355, ('Hillary Clinton', 'l'): 1, ('Linda Dewan', 'Hillary Clinton'): 1, ('Hillary Clinton', 'Capricia Marshall'): 9, ('Phillip Crowley', 'Hillary Clinton'): 2, ('Cheryl Mills', 'Anne-Marie Slaughter'): 1}

G = nx.Graph()

# To add the a node for each of the email parties:
srcs, dests = zip(* [(fr, to) for (fr, to) in hrc_dict.keys()])
G.add_nodes_from(srcs + dests)
# (but it isn't needed IF the following method is used
#  to add the edges, since add_edge also creates the nodes if
#  they don't yet exist)

# note the use of the attribute "weight" here
for (s,r), count in hrc_dict.items():
    G.add_edge(s, r, weight=count)

# produce info to draw:
# a) if weight was used above, spring_layout takes 
#    into account the edge strengths
pos = nx.spring_layout(G)

# b) specifiy edge labels explicitly
# method from https://groups.google.com/forum/#!topic/networkx-discuss/hw3OVBF8orc
edge_labels=dict([((u,v,),d['weight'])
             for u,v,d in G.edges(data=True)])

# draw it
plt.figure(1);
nx.draw_networkx(G, pos, with_labels=True)
nx.draw_networkx_edge_labels(G,pos,edge_labels=edge_labels)

plt.axis('equal') # spring weighting makes more sense this way
plt.show()

And this is what we might see:

Example output - note that because the HC/A-MS edge is so strong it is very short

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Add nodes & edges in graph via networkx and using python

From Dev

How to add labels to nodes in a graph in networkx?

From Dev

How to align nodes and edges in networkx

From Dev

NetworkX: add edges to graph from node attributes

From Dev

NetworkX: add edges to a graph from a shapefile

From Java

How to convert a bipartite list of edges into a graph in NetworkX with each set of nodes having overlapping labels?

From Dev

How to access keys in nodes and edges in NetworkX MultiDiGraph

From Dev

How to graph nodes on a grid in networkx

From Dev

How to add edge weights from one graph to matching edges in a different graph in NetworkX?

From Dev

How to add edge weights from one graph to matching edges in a different graph in NetworkX?

From Dev

How to find the number of edges bewteen any two nodes using networkx?

From Dev

Boost graph: How to copy the nodes and edges of a graph without copying properties?

From Dev

Networkx : How to create graph edges from a csv file?

From Dev

Filter NetworkX graph to list all edges coming from a nodes with specific attributes

From Dev

DSE Graph with Java Driver, how to add edges

From Dev

How to find two randoms nodes with no edges between them in graph?

From Dev

How to find two randoms nodes with no edges between them in graph?

From Dev

In Matlab, how can I remove nodes with no edges in a directed graph?

From Dev

Drawing network with nodes and edges in python

From Dev

Drawing only Djikstra's Nodes and Edges Networkx

From Dev

How to determine specific color and size of chosen nodes in Networkx graph plot

From Dev

How to make NetworkX graph from Delaunay preserving attributes of the input nodes?

From Dev

Fetch connected nodes in a NetworkX graph

From Dev

R/Network Analysis - How to create edges by node's attributes

From Dev

List of edges that don't exist in a networkx graph?

From Dev

python networkx - mark edges by coloring for graph drawing

From Dev

List of edges that don't exist in a networkx graph?

From Dev

CytoscapeJS network graph - Edges are not plotted

From Dev

Python: Networkx get path between given two nodes with nodes and edges

Related Related

  1. 1

    Add nodes & edges in graph via networkx and using python

  2. 2

    How to add labels to nodes in a graph in networkx?

  3. 3

    How to align nodes and edges in networkx

  4. 4

    NetworkX: add edges to graph from node attributes

  5. 5

    NetworkX: add edges to a graph from a shapefile

  6. 6

    How to convert a bipartite list of edges into a graph in NetworkX with each set of nodes having overlapping labels?

  7. 7

    How to access keys in nodes and edges in NetworkX MultiDiGraph

  8. 8

    How to graph nodes on a grid in networkx

  9. 9

    How to add edge weights from one graph to matching edges in a different graph in NetworkX?

  10. 10

    How to add edge weights from one graph to matching edges in a different graph in NetworkX?

  11. 11

    How to find the number of edges bewteen any two nodes using networkx?

  12. 12

    Boost graph: How to copy the nodes and edges of a graph without copying properties?

  13. 13

    Networkx : How to create graph edges from a csv file?

  14. 14

    Filter NetworkX graph to list all edges coming from a nodes with specific attributes

  15. 15

    DSE Graph with Java Driver, how to add edges

  16. 16

    How to find two randoms nodes with no edges between them in graph?

  17. 17

    How to find two randoms nodes with no edges between them in graph?

  18. 18

    In Matlab, how can I remove nodes with no edges in a directed graph?

  19. 19

    Drawing network with nodes and edges in python

  20. 20

    Drawing only Djikstra's Nodes and Edges Networkx

  21. 21

    How to determine specific color and size of chosen nodes in Networkx graph plot

  22. 22

    How to make NetworkX graph from Delaunay preserving attributes of the input nodes?

  23. 23

    Fetch connected nodes in a NetworkX graph

  24. 24

    R/Network Analysis - How to create edges by node's attributes

  25. 25

    List of edges that don't exist in a networkx graph?

  26. 26

    python networkx - mark edges by coloring for graph drawing

  27. 27

    List of edges that don't exist in a networkx graph?

  28. 28

    CytoscapeJS network graph - Edges are not plotted

  29. 29

    Python: Networkx get path between given two nodes with nodes and edges

HotTag

Archive