Source Information¶
Created by:
Updated by: October 25, 2024 by Gloria Seo
Resources: https://networkx.github.io/documentation/stable/tutorial.html
Goal¶
This notebook purpose to learn the basic of NetworkX and show an example of the usages of making networks, save the layout, and analyze them. NetworkX is a Python library specifically designed for the creation, manipulation, and study of complex networks (graphs). It provides tools to model various types of graphs, analyze their properties, and visualize them.
Required Modules for the Jupyter Notebook¶
Before running the following notebook, make sure to import the following modules.
Module: matplotlib,networkx
import matplotlib.pyplot as plt #matplotlib
import networkx as nx #networkX
from networkx.drawing.nx_pydot import write_dot #writing stuff
import networkx as nx
Creating your first graph using NetworkX¶
Start off by creating a graph with nx.Graph(). This graph will have no nodes or edges and should be empty if you draw it.
g = nx.Graph()
Now let's add some nodes to this graph. Nodes are like the corners or endpoints of the graph.
g.add_node(1)
or a list of nodes
g.add_nodes_from([2,3])
Now let's add some edges. Right now we should just have three points on a graph. Well, edges are the lines that connect the nodes. So let's add some.
g.add_edge(1, 2)
g.add_edges_from([(2, 3), (1, 3)])
Now let's check the statistics of the graph. The following commands will go in order with the output.
g.number_of_nodes() #number of nodes
3
g.number_of_edges() #number of edges
3
list(g.nodes) #list out your node names
[1, 2, 3]
list(g.edges) #list out your node edges
[(1, 2), (1, 3), (2, 3)]
list(g.adj[1]) #list out the nodes adjacent to the specified node
[2, 3]
g.degree[1] #list out the number of edges connected to the node
2
Now let's say you add an extra node or you want to collapse your graph.
g.add_node(10)
You can delete the node or edge by doing the same command you did to add it and replacing "add" with "remove"
g.remove_node(10)
Attributes¶
Every node or edge can have an attribute that basically is a description of the node or edge. Let's start off with a new graph.
f = nx.Graph(month="August")
f.graph
{'month': 'August'}
Now let's add some nodes so we can put attributes on them
f.add_node(1, day ='Monday')
f.add_nodes_from([2],day='Tuesday')
f.nodes[1]
f.nodes[1]['food'] = "Pizza"
f.nodes.data()
NodeDataView({1: {'day': 'Monday', 'food': 'Pizza'}, 2: {'day': 'Tuesday'}})
Now lets make some edges and add attributes to those.
f.add_nodes_from([3,5])
f.add_edge(1,2,profit=310)
f.add_edges_from([(1,3),(2,3)], profit=-10)
f.add_edges_from([(1, 5, {'Money': 0}), (2, 4, {'Profit': 8})])
f[1][5]['money'] = 85
f.edges.data()
EdgeDataView([(1, 2, {'profit': 310}), (1, 3, {'profit': -10}), (1, 5, {'Money': 0, 'money': 85}), (2, 3, {'profit': -10}), (2, 4, {'Profit': 8})])
Printing out the graph¶
This section will go over how to print out the networks you have made with matplotlib's pyplot tool and nx.draw() Lets start out with a small subplot.
plt.subplot(121)
<AxesSubplot:>
Now lets draw the model we created in one of the earlier sections. The nx.draw command has a few constraints where you can put labels and change fonts, with much more. Some of it is explained in the later sections.
nx.draw(g, with_labels=True, font_weight='bold')
You can load in preset graphs from a list on this site https://networkx.github.io/documentation/stable/tutorial.html. You can limit the range of the network with the nlist which helps you narrow down the network.
G = nx.petersen_graph() #loading in a default preset graph
nx.draw_shell(G, nlist=[range(5, 10), range(5)], with_labels=True, font_weight='bold')
nx.draw_shell allows you to draw smaller shells that dont display the whole thing.
B = nx.dodecahedral_graph()
shells = [[2, 3, 4, 5, 6], [8, 1, 0, 19, 18, 17, 16, 15, 14, 7], [9, 10, 11, 12, 13]]
nx.draw_shell(B, nlist=shells, with_labels=True, font_weight='bold')
Saving Files¶
We have all of these cool looking graphs! But we need to save them to our computer. How do we do that? The plt.savefig() command does its job.
nx.draw(B)
plt.savefig('image.png')
Summary¶
You should now be able to create nodes and edges and add attributes in them. If you want to see an interactive model, either go to this link:https://networkx.github.io/documentation/stable/tutorial.html or download Cytoscape from https://cytoscape.org/ and open up you files using the application. Congratualations, you have now started on the path of network data.
Submit Ticket¶
If you find anything that needs to be changed, edited, or if you would like to provide feedback or contribute to the notebook, please submit a ticket by contacting us at:
Email: consult@sdsc.edu
We appreciate your input and will review your suggestions promptly!