Last active
February 24, 2017 17:09
-
-
Save DavidRdgz/40f8d3c569a3f9b57e4a2efaa5ffca51 to your computer and use it in GitHub Desktop.
time-dependent graphs
This file contains 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
import networkx as nx | |
import matplotlib.pyplot as plt | |
import matplotlib | |
# -------------- Get Data/Graphs ------------- | |
with open('graph-data/g1.txt', 'r') as g: | |
g1 = map(lambda x: eval(x), g.readlines()) | |
with open('graph-data/g2.txt', 'r') as g: | |
g2 = map(lambda x: eval(x), g.readlines()) | |
def make_edge(g): | |
return map(lambda x: (x['domsuf'], x['host'], {'weight': x['count']}), g) | |
df1 = make_edge(g1) | |
df2 = make_edge(g2) | |
# -------------- Setup Graphs -------------- | |
G = nx.MultiGraph() | |
H = nx.MultiGraph() | |
G.add_edges_from(df1) | |
H.add_edges_from(df2) | |
posg = nx.spring_layout(G) | |
xy1 = posg['A'] | |
xy2 = posg['B'] | |
posh = nx.spring_layout(H) | |
yx1 = posh['A'] | |
yx2 = posh['B'] | |
line_crossings = [[xy1, yx1], [xy2, yx2]] | |
# --------- Setup Subplots ----------------- | |
fig = plt.figure(figsize=(10*2, 5*2)) | |
ax1 = fig.add_subplot(121) | |
ax2 = fig.add_subplot(122) | |
print G.nodes() | |
plt.sca(ax1) | |
ax1.set_title('$T_i$') | |
nx.draw_networkx_edges(G, posg, width=1.0, alpha=0.5) | |
labels = dict(map(lambda x: (x, '$' + x + '$'), G.nodes())) | |
nx.draw_networkx_labels(G, posg, labels, font_size=8) | |
plt.sca(ax2) | |
ax2.set_title('$T_{i+1}$') | |
nx.draw_networkx_edges(H, posh, width=1.0, alpha=0.5) | |
labels = dict(map(lambda x: (x, '$' + x + '$'), H.nodes())) | |
nx.draw_networkx_labels(H, posh, labels, font_size=8) | |
transFigure = fig.transFigure.inverted() | |
for a, b in line_crossings: | |
coord1 = transFigure.transform(ax1.transData.transform([a[0], a[1]])) | |
coord2 = transFigure.transform(ax2.transData.transform([b[0], b[1]])) | |
line = matplotlib.lines.Line2D((coord1[0], coord2[0]), (coord1[1], coord2[1]), | |
transform=fig.transFigure, color='black', linestyle='--', linewidth=0.5) | |
fig.lines.append(line) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment