Created
September 29, 2021 08:44
-
-
Save MJacobs1985/759282529a724d1e55b60fcf7f27363f to your computer and use it in GitHub Desktop.
Network Graphs for Epidemiology
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Visualization | |
import networkx as nx | |
import matplotlib.pyplot as plt | |
import warnings | |
warnings.filterwarnings("ignore") | |
%matplotlib inline | |
# Network topology | |
g = nx.random_geometric_graph(1000, 0.1, seed=896803) # Random Clustered graph 1000 nodes - distance =0.05, no self loops | |
pos = nx.get_node_attributes(g, "pos") | |
plt.figure(figsize=(16, 16)) | |
dmin = 1 | |
ncenter = 0 | |
for n in pos: | |
x, y = pos[n] | |
d = (x - 0.5) ** 2 + (y - 0.5) ** 2 | |
if d < dmin: | |
ncenter = n | |
dmin = d | |
# color by path length from node near center | |
p = dict(nx.single_source_shortest_path_length(g, ncenter)) | |
plt.figure(figsize=(16, 16)) | |
nx.draw_networkx_edges(g, pos, alpha=0.2, edge_color="black") | |
nx.draw_networkx_nodes( | |
g, | |
pos, | |
nodelist=list(p.keys()), | |
node_size=80, | |
node_color=list(p.values()), | |
cmap=plt.cm.Reds_r, | |
alpha=0.8 | |
) | |
plt.xlim(-0.05, 1.05) | |
plt.ylim(-0.05, 1.05) | |
plt.axis("off") | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment