Advertisement
Guest User

Untitled

a guest
Mar 12th, 2024
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. %matplotlib notebook
  2.  
  3. from operator import itemgetter
  4. import matplotlib.pyplot as plt
  5. import networkx as nx
  6. import numpy as np
  7. from matplotlib.animation import FuncAnimation, PillowWriter
  8.  
  9. G = nx.Graph()
  10.  
  11. G.add_edge("a", "b", weight=0.6)
  12. G.add_edge("a", "c", weight=0.2)
  13. G.add_edge("c", "d", weight=0.1)
  14. G.add_edge("c", "e", weight=0.7)
  15. G.add_edge("c", "f", weight=0.9)
  16. G.add_edge("a", "d", weight=0.3)
  17.  
  18. init_pos = nx.spring_layout(G, seed=7)
  19.  
  20. def movedge(pos, p1, p2, distance):
  21. nodes = p1, p2
  22. p1, p2 = itemgetter(p1, p2)(pos)
  23. curr_distance = np.linalg.norm(p2 - p1)
  24. scale = distance / curr_distance
  25. new_p1 = p1 + (p2 - p1) * scale
  26. new_p2 = p2 + (p1 - p2) * scale
  27. return {**pos, **dict(zip(nodes, (new_p1, new_p2)))}
  28.  
  29. def drawg(g, curr_pos, ax):
  30. ax.set(xlim=(-3, 3), ylim=(-1, 1))
  31. # NODES
  32. nx.draw_networkx_nodes(
  33. g, curr_pos, ax=ax,
  34. node_color="tab:red",
  35. edgecolors="tab:gray",
  36. node_size=400, alpha=0.9,
  37. )
  38. nx.draw_networkx_labels(
  39. g, curr_pos, ax=ax,
  40. font_size=22, font_color="whitesmoke",
  41. )
  42. # EDGES
  43. nx.draw_networkx_edges(
  44. g, curr_pos, ax=ax,
  45. width=4, alpha=0.7,
  46. edge_color="tab:blue", style="dashed",
  47. )
  48. nx.draw_networkx_edge_labels(
  49. g, curr_pos, ax=ax,
  50. font_size=10, font_weight="bold",
  51. edge_labels=nx.get_edge_attributes(g, "weight"),
  52. )
  53.  
  54.  
  55. fig, ax = plt.subplots(figsize=(10, 6))
  56.  
  57. moves = ( # some random moves
  58. {"p1": "a", "p2": "b", "distance": 0.57},
  59. {"p1": "c", "p2": "d", "distance": 0.12},
  60. {"p1": "c", "p2": "f", "distance": 0.88},
  61. {"p1": "c", "p2": "d", "distance": 0.07},
  62. {"p1": "c", "p2": "e", "distance": 0.68},
  63. {"p1": "a", "p2": "b", "distance": 0.59},
  64. )
  65.  
  66. N = 20
  67. frames = {}
  68. H = G.copy(); new_pos = init_pos.copy()
  69. for move, rng in zip(moves, np.arange(1, len(moves) * N + 1).reshape(-1, N)):
  70. p1, p2 = move["p1"], move["p2"]; init = H[p1][p2]["weight"]
  71. for i, j in zip(rng, np.linspace(init, move["distance"], N).round(2)):
  72. H[p1][p2]["weight"] = j; move["distance"] = j-init
  73. new_pos.update(movedge(**move, pos=new_pos))
  74. frames[i] = {"g": H.copy(), "curr_pos": movedge(**move, pos=new_pos)}
  75.  
  76.  
  77. def animate(i):
  78. ax.clear()
  79. drawg(**frames[i + 1], ax=ax)
  80.  
  81.  
  82. ani = FuncAnimation(
  83. fig, animate, frames=N * len(moves), interval=150, repeat=True
  84. )
  85.  
  86. writer = PillowWriter(
  87. fps=15, metadata=dict(artist="Me"), bitrate=1800,
  88. )
  89. ani.save("output.gif", writer=writer)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement