Медленный рендеринг пути к графу с использованием анимации python matplotlib

Я вычисляю кратчайшие пути графов с помощью Python Networkx и Matplotlib. Я сосредоточен на анимации проблема проверки маршрута путь в частности здесь. Моя цель – плавно визуализировать такую ​​анимацию, чтобы визуально смотреть на путь и в конечном итоге сохранить анимацию в mp4 через FFmpeg (но это уже другая тема).

Моя проблема в том, что анимация совсем не гладкая для графика и пути, над которыми я сейчас работаю: 418 узлов, 558 ребер и путь из 783 шагов.

Учитывая, что у меня быстрый компьютер, я считаю, что мой код можно явно улучшить или, что маловероятно, я достиг предела matplotlib для моего варианта использования.

Приведенный ниже код работает правильно, но, как вы можете видеть, он рисует путь медленно и медленнее на каждом шаге пути. Думаю, чтобы нарисовать 300 шагов, мне нужно около 30 футов.

Я видел, что код работает быстрее, если я теперь показываю метки узлов, поэтому они не отображаются с этим кодом.

Поскольку я хочу изменить только цвет ребер и узлов, может быть более эффективный подход, чем мой, поскольку я считаю, что рисую весь граф на каждом шаге. Я не знаю, как просто изменить цвет узлов / краев, не перерисовывая все, потому что я новичок во всем этом.

Вот анимация пути в приведенном ниже сценарии, который я сгенерировал по-другому: (Потребовалось около 3 часов … для запуска сценария ниже, делая по одному снимку на каждом шаге, который я использовал для создания этого плавного видео)
https://www.youtube.com/watch?v=-ObZcSWGhdM

Код:

# Import libraries
import networkx as nx
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time


# Define Graph
G = nx.Graph()
G.add_edges_from([(1, 3, {'weight': 99}), (2, 3, {'weight': 73}), (3, 4, {'weight': 45}), (4, 104, {'weight': 166}), (4, 6, {'weight': 89}), (6, 103, {'weight': 127}), (103, 104, {'weight': 76}), (104, 106, {'weight': 45}), (106, 108, {'weight': 54}), (108, 110, {'weight': 49}), (110, 115, {'weight': 39}), (110, 111, {'weight': 85}), (111, 113, {'weight': 49}), (111, 114, {'weight': 31}), (114, 115, {'weight': 82}), (108, 109, {'weight': 203}), (106, 107, {'weight': 218}), (104, 105, {'weight': 229}), (105, 107, {'weight': 47}), (107, 109, {'weight': 69}), (5, 6, {'weight': 222}), (5, 8, {'weight': 49}), (7, 8, {'weight': 28}), (8, 13, {'weight': 107}), (13, 14, {'weight': 80}), (12, 13, {'weight': 80}), (14, 15, {'weight': 81}), (15, 10, {'weight': 63}), (9, 10, {'weight': 33}), (10, 11, {'weight': 127}), (6, 11, {'weight': 96}), (11, 17, {'weight': 68}), (26, 103, {'weight': 213}), (15, 16, {'weight': 69}), (16, 17, {'weight': 62}), (17, 25, {'weight': 76}), (16, 23, {'weight': 102}), (23, 24, {'weight': 31}), (24, 31, {'weight': 25}), (22, 23, {'weight': 75}), (22, 15, {'weight': 112}), (13, 20, {'weight': 124}), (14, 21, {'weight': 116}), (20, 21, {'weight': 79}), (21, 22, {'weight': 77}), (18, 19, {'weight': 74}), (19, 20, {'weight': 80}), (12, 19, {'weight': 119}), (35, 22, {'weight': 99}), (34, 35, {'weight': 15}), (33, 34, {'weight': 137}), (32, 33, {'weight': 46}), (29, 32, {'weight': 39}), (29, 100, {'weight': 181}), (100, 101, {'weight': 51}), (27, 101, {'weight': 162}), (27, 28, {'weight': 31}), (26, 27, {'weight': 36}), (25, 26, {'weight': 19}), (25, 30, {'weight': 30}), (28, 30, {'weight': 20}), (28, 29, {'weight': 50}), (32, 51, {'weight': 197}), (50, 51, {'weight': 99}), (33, 50, {'weight': 188}), (46, 50, {'weight': 150}), (44, 46, {'weight': 75}), (44, 45, {'weight': 52}), (35, 44, {'weight': 114}), (43, 46, {'weight': 71}), (35, 36, {'weight': 28}), (36, 37, {'weight': 16}), (37, 38, {'weight': 123}), (38, 40, {'weight': 95}), (40, 41, {'weight': 85}), (40, 42, {'weight': 86}), (38, 39, {'weight': 114}), (20, 38, {'weight': 100}), (40, 36, {'weight': 154}), (31, 34, {'weight': 131}), (30, 31, {'weight': 36}), (99, 100, {'weight': 47}), (32, 99, {'weight': 182}), (97, 98, {'weight': 188}), (98, 99, {'weight': 1}), (94, 97, {'weight': 46}), (93, 94, {'weight': 41}), (92, 93, {'weight': 62}), (91, 92, {'weight': 14}), (92, 95, {'weight': 34}), (94, 95, {'weight': 46}), (91, 96, {'weight': 72}), (56, 91, {'weight': 91}), (51, 56, {'weight': 87}), (55, 56, {'weight': 81}), (49, 50, {'weight': 63}), (48, 49, {'weight': 53}), (49, 53, {'weight': 104}), (53, 55, {'weight': 128}), (52, 53, {'weight': 68}), (46, 47, {'weight': 69}), (53, 54, {'weight': 76}), (55, 61, {'weight': 132}), (61, 87, {'weight': 63}), (82, 87, {'weight': 105}), (82, 88, {'weight': 42}), (88, 93, {'weight': 121}), (88, 89, {'weight': 97}), (89, 90, {'weight': 56}), (55, 90, {'weight': 70}), (86, 87, {'weight': 31}), (85, 86, {'weight': 48}), (59, 86, {'weight': 69}), (59, 60, {'weight': 55}), (59, 61, {'weight': 44}), (57, 59, {'weight': 67}), (58, 59, {'weight': 47}), (59, 69, {'weight': 187}), (417, 69, {'weight': 59}), (68, 69, {'weight': 9}), (68, 70, {'weight': 16}), (70, 78, {'weight': 67}), (70, 71, {'weight': 73}), (71, 72, {'weight': 34}), (72, 73, {'weight': 21}), (72, 74, {'weight': 21}), (71, 76, {'weight': 69}), (76, 77, {'weight': 69}), (77, 81, {'weight': 36}), (80, 81, {'weight': 16}), (81, 82, {'weight': 18}), (82, 83, {'weight': 22}), (80, 83, {'weight': 20}), (83, 85, {'weight': 47}), (84, 85, {'weight': 58}), (78, 84, {'weight': 33}), (78, 79, {'weight': 33}), (79, 80, {'weight': 50}), (75, 76, {'weight': 69}), (77, 384, {'weight': 104}), (384, 119, {'weight': 12}), (119, 382, {'weight': 50}), (384, 383, {'weight': 53}), (382, 381, {'weight': 69}), (75, 381, {'weight': 88}), (381, 118, {'weight': 10}), (118, 380, {'weight': 34}), (379, 380, {'weight': 76}), (118, 386, {'weight': 152}), (385, 386, {'weight': 74}), (66, 385, {'weight': 69}), (66, 67, {'weight': 20}), (66, 64, {'weight': 51}), (64, 65, {'weight': 22}), (63, 64, {'weight': 41}), (75, 385, {'weight': 157}), (71, 66, {'weight': 87}), (62, 67, {'weight': 83}), (67, 68, {'weight': 93}), (382, 383, {'weight': 14}), (372, 383, {'weight': 76}), (379, 377, {'weight': 25}), (377, 378, {'weight': 40}), (386, 378, {'weight': 67}), (378, 375, {'weight': 55}), (375, 376, {'weight': 29}), (343, 375, {'weight': 74}), (374, 375, {'weight': 61}), (374, 344, {'weight': 59}), (374, 373, {'weight': 41}), (373, 345, {'weight': 122}), (373, 372, {'weight': 83}), (343, 345, {'weight': 89}), (343, 376, {'weight': 74}), (112, 376, {'weight': 27}), (112, 375, {'weight': 40}), (112, 342, {'weight': 39}), (341, 342, {'weight': 35}), (340, 341, {'weight': 42}), (342, 387, {'weight': 79}), (387, 388, {'weight': 34}), (388, 389, {'weight': 116}), (388, 391, {'weight': 94}), (391, 392, {'weight': 39}), (371, 372, {'weight': 55}), (370, 371, {'weight': 42}), (367, 372, {'weight': 165}), (371, 346, {'weight': 115}), (345, 346, {'weight': 96}), (346, 347, {'weight': 74}), (347, 117, {'weight': 6}), (117, 352, {'weight': 75}), (346, 351, {'weight': 157}), (366, 352, {'weight': 74}), (351, 352, {'weight': 20}), (366, 358, {'weight': 157}), (368, 359, {'weight': 152}), (359, 360, {'weight': 24}), (360, 361, {'weight': 62}), (361, 362, {'weight': 28}), (361, 126, {'weight': 86}), (125, 126, {'weight': 77}), (102, 125, {'weight': 69}), (126, 124, {'weight': 210}), (126, 127, {'weight': 66}), (127, 128, {'weight': 35}), (127, 360, {'weight': 94}), (358, 359, {'weight': 31}), (357, 358, {'weight': 28}), (356, 357, {'weight': 23}), (355, 356, {'weight': 22}), (354, 355, {'weight': 63}), (353, 354, {'weight': 21}), (353, 364, {'weight': 64}), (364, 365, {'weight': 12}), (357, 363, {'weight': 51}), (351, 241, {'weight': 198}), (241, 242, {'weight': 13}), (128, 242, {'weight': 80}), (363, 242, {'weight': 50}), (366, 367, {'weight': 29}), (367, 369, {'weight': 22}), (368, 369, {'weight': 6}), (366, 368, {'weight': 26}), (120, 369, {'weight': 20}), (120, 401, {'weight': 104}), (120, 410, {'weight': 93}), (401, 410, {'weight': 104}), (410, 411, {'weight': 16}), (416, 411, {'weight': 133}), (393, 416, {'weight': 30}), (93, 416, {'weight': 71}), (393, 394, {'weight': 85}), (97, 394, {'weight': 39}), (394, 418, {'weight': 78}), (98, 395, {'weight': 106}), (395, 396, {'weight': 17}), (396, 399, {'weight': 36}), (399, 400, {'weight': 57}), (125, 400, {'weight': 89}), (398, 400, {'weight': 29}), (397, 398, {'weight': 23}), (396, 397, {'weight': 28}), (398, 399, {'weight': 33}), (418, 405, {'weight': 24}), (405, 406, {'weight': 24}), (406, 408, {'weight': 28}), (418, 407, {'weight': 31}), (407, 408, {'weight': 20}), (408, 409, {'weight': 19}), (393, 409, {'weight': 81}), (409, 412, {'weight': 39}), (412, 413, {'weight': 17}), (413, 414, {'weight': 15}), (402, 414, {'weight': 78}), (402, 403, {'weight': 45}), (403, 404, {'weight': 26}), (404, 412, {'weight': 34}), (403, 406, {'weight': 45}), (362, 402, {'weight': 57}), (401, 402, {'weight': 18}), (400, 362, {'weight': 57}), (397, 405, {'weight': 40}), (411, 413, {'weight': 21}), (101, 102, {'weight': 104}), (102, 105, {'weight': 288}), (109, 123, {'weight': 136}), (123, 131, {'weight': 120}), (123, 124, {'weight': 142}), (124, 129, {'weight': 73}), (129, 130, {'weight': 72}), (130, 131, {'weight': 76}), (130, 242, {'weight': 202}), (130, 132, {'weight': 150}), (114, 121, {'weight': 231}), (121, 141, {'weight': 35}), (121, 122, {'weight': 58}), (122, 140, {'weight': 41}), (240, 241, {'weight': 44}), (239, 240, {'weight': 93}), (240, 282, {'weight': 71}), (282, 281, {'weight': 77}), (281, 280, {'weight': 34}), (281, 286, {'weight': 87}), (286, 285, {'weight': 30}), (280, 285, {'weight': 82}), (239, 280, {'weight': 72}), (282, 283, {'weight': 51}), (283, 284, {'weight': 31}), (350, 283, {'weight': 65}), (350, 287, {'weight': 65}), (287, 286, {'weight': 17}), (285, 289, {'weight': 53}), (289, 301, {'weight': 123}), (301, 303, {'weight': 49}), (303, 302, {'weight': 55}), (303, 346, {'weight': 85}), (287, 288, {'weight': 72}), (350, 349, {'weight': 60}), (349, 348, {'weight': 21}), (350, 351, {'weight': 31}), (239, 132, {'weight': 203}), (132, 133, {'weight': 178}), (133, 135, {'weight': 100}), (134, 135, {'weight': 37}), (135, 136, {'weight': 41}), (135, 140, {'weight': 221}), (138, 139, {'weight': 112}), (139, 140, {'weight': 25}), (140, 141, {'weight': 52}), (141, 142, {'weight': 39}), (142, 144, {'weight': 68}), (143, 144, {'weight': 55}), (144, 145, {'weight': 38}), (136, 137, {'weight': 80}), (136, 183, {'weight': 41}), (183, 181, {'weight': 93}), (181, 182, {'weight': 21}), (182, 184, {'weight': 98}), (183, 184, {'weight': 22}), (184, 148, {'weight': 113}), (184, 185, {'weight': 73}), (183, 189, {'weight': 80}), (148, 149, {'weight': 39}), (149, 150, {'weight': 54}), (150, 154, {'weight': 81}), (154, 158, {'weight': 77}), (158, 159, {'weight': 45}), (157, 158, {'weight': 96}), (156, 157, {'weight': 49}), (155, 156, {'weight': 59}), (152, 156, {'weight': 28}), (152, 153, {'weight': 40}), (153, 154, {'weight': 53}), (149, 153, {'weight': 76}), (148, 151, {'weight': 54}), (151, 152, {'weight': 29}), (185, 150, {'weight': 90}), (185, 186, {'weight': 18}), (186, 187, {'weight': 25}), (187, 188, {'weight': 13}), (188, 189, {'weight': 21}), (142, 181, {'weight': 161}), (182, 144, {'weight': 173}), (189, 185, {'weight': 48}), (147, 148, {'weight': 91}), (146, 147, {'weight': 51}), (301, 304, {'weight': 28}), (304, 321, {'weight': 173}), (320, 321, {'weight': 11}), (320, 325, {'weight': 55}), (323, 325, {'weight': 84}), (322, 323, {'weight': 41}), (325, 326, {'weight': 53}), (324, 326, {'weight': 82}), (316, 320, {'weight': 154}), (315, 316, {'weight': 51}), (316, 317, {'weight': 44}), (317, 318, {'weight': 91}), (317, 328, {'weight': 109}), (328, 333, {'weight': 93}), (326, 327, {'weight': 110}), (315, 329, {'weight': 215}), (329, 330, {'weight': 69}), (330, 390, {'weight': 89}), (330, 337, {'weight': 66}), (332, 337, {'weight': 68}), (332, 331, {'weight': 29}), (328, 331, {'weight': 36}), (331, 334, {'weight': 155}), (326, 334, {'weight': 82}), (334, 335, {'weight': 30}), (332, 335, {'weight': 151}), (336, 339, {'weight': 60}), (339, 340, {'weight': 21}), (340, 376, {'weight': 15}), (337, 341, {'weight': 112}), (337, 338, {'weight': 128}), (335, 339, {'weight': 24}), (319, 320, {'weight': 28}), (307, 319, {'weight': 34}), (306, 307, {'weight': 82}), (305, 319, {'weight': 125}), (307, 308, {'weight': 53}), (308, 309, {'weight': 60}), (309, 310, {'weight': 50}), (309, 311, {'weight': 68}), (308, 312, {'weight': 85}), (314, 316, {'weight': 70}), (299, 301, {'weight': 39}), (299, 300, {'weight': 40}), (298, 299, {'weight': 53}), (298, 297, {'weight': 147}), (294, 297, {'weight': 63}), (294, 295, {'weight': 19}), (295, 296, {'weight': 89}), (296, 266, {'weight': 131}), (266, 265, {'weight': 16}), (265, 264, {'weight': 41}), (264, 267, {'weight': 120}), (289, 290, {'weight': 86}), (290, 291, {'weight': 102}), (291, 292, {'weight': 30}), (292, 293, {'weight': 42}), (293, 294, {'weight': 37}), (270, 295, {'weight': 184}), (296, 268, {'weight': 128}), (267, 268, {'weight': 37}), (268, 269, {'weight': 66}), (269, 270, {'weight': 26}), (270, 271, {'weight': 15}), (271, 272, {'weight': 28}), (272, 273, {'weight': 14}), (273, 274, {'weight': 27}), (274, 247, {'weight': 39}), (247, 246, {'weight': 39}), (245, 246, {'weight': 29}), (245, 244, {'weight': 31}), (244, 243, {'weight': 22}), (239, 243, {'weight': 73}), (244, 279, {'weight': 110}), (278, 279, {'weight': 58}), (290, 279, {'weight': 96}), (277, 278, {'weight': 46}), (277, 291, {'weight': 97}), (277, 247, {'weight': 107}), (246, 278, {'weight': 104}), (274, 275, {'weight': 78}), (272, 293, {'weight': 196}), (292, 276, {'weight': 89}), (262, 266, {'weight': 88}), (262, 263, {'weight': 38}), (261, 262, {'weight': 74}), (260, 261, {'weight': 43}), (259, 260, {'weight': 28}), (256, 259, {'weight': 81}), (258, 259, {'weight': 34}), (258, 257, {'weight': 53}), (257, 265, {'weight': 74}), (254, 257, {'weight': 89}), (254, 255, {'weight': 37}), (254, 253, {'weight': 49}), (254, 252, {'weight': 74}), (252, 264, {'weight': 51}), (252, 227, {'weight': 69}), (226, 227, {'weight': 40}), (227, 228, {'weight': 137}), (228, 229, {'weight': 128}), (229, 230, {'weight': 67}), (230, 231, {'weight': 64}), (231, 232, {'weight': 69}), (228, 267, {'weight': 100}), (251, 269, {'weight': 72}), (250, 271, {'weight': 107}), (250, 229, {'weight': 28}), (223, 229, {'weight': 118}), (223, 224, {'weight': 38}), (224, 225, {'weight': 141}), (224, 228, {'weight': 106}), (225, 227, {'weight': 88}), (220, 221, {'weight': 65}), (221, 222, {'weight': 84}), (222, 223, {'weight': 66}), (223, 217, {'weight': 107}), (217, 218, {'weight': 47}), (218, 219, {'weight': 36}), (218, 415, {'weight': 88}), (216, 217, {'weight': 72}), (215, 216, {'weight': 83}), (222, 216, {'weight': 90}), (215, 221, {'weight': 79}), (221, 230, {'weight': 66}), (230, 249, {'weight': 93}), (248, 249, {'weight': 64}), (248, 231, {'weight': 105}), (247, 248, {'weight': 52}), (273, 249, {'weight': 50}), (220, 231, {'weight': 53}), (213, 220, {'weight': 72}), (213, 214, {'weight': 30}), (213, 232, {'weight': 42}), (232, 245, {'weight': 171}), (243, 237, {'weight': 55}), (237, 238, {'weight': 77}), (237, 236, {'weight': 54}), (212, 236, {'weight': 102}), (212, 211, {'weight': 20}), (235, 236, {'weight': 91}), (235, 238, {'weight': 55}), (238, 239, {'weight': 55}), (234, 235, {'weight': 33}), (234, 233, {'weight': 59}), (233, 211, {'weight': 50}), (212, 213, {'weight': 52}), (214, 215, {'weight': 141}), (210, 211, {'weight': 35}), (209, 210, {'weight': 68}), (209, 208, {'weight': 22}), (208, 207, {'weight': 10}), (207, 206, {'weight': 16}), (206, 205, {'weight': 19}), (205, 204, {'weight': 30}), (205, 194, {'weight': 66}), (206, 193, {'weight': 47}), (192, 207, {'weight': 66}), (191, 209, {'weight': 64}), (116, 191, {'weight': 17}), (190, 116, {'weight': 13}), (116, 192, {'weight': 82}), (192, 193, {'weight': 55}), (188, 190, {'weight': 103}), (193, 194, {'weight': 69}), (194, 195, {'weight': 105}), (203, 204, {'weight': 122}), (203, 195, {'weight': 63}), (202, 203, {'weight': 39}), (202, 201, {'weight': 87}), (201, 200, {'weight': 97}), (202, 196, {'weight': 58}), (195, 196, {'weight': 40}), (196, 197, {'weight': 85}), (197, 198, {'weight': 94}), (198, 200, {'weight': 25}), (199, 200, {'weight': 64}), (198, 177, {'weight': 48}), (197, 201, {'weight': 47}), (176, 177, {'weight': 93}), (176, 197, {'weight': 55}), (176, 178, {'weight': 33}), (178, 179, {'weight': 56}), (178, 180, {'weight': 46}), (175, 176, {'weight': 86}), (174, 175, {'weight': 40}), (175, 196, {'weight': 56}), (173, 174, {'weight': 69}), (174, 195, {'weight': 56}), (172, 173, {'weight': 116}), (173, 169, {'weight': 62}), (169, 170, {'weight': 52}), (170, 171, {'weight': 76}), (168, 169, {'weight': 123}), (168, 172, {'weight': 65}), (187, 172, {'weight': 104}), (172, 193, {'weight': 60}), (166, 170, {'weight': 98}), (166, 167, {'weight': 20}), (165, 166, {'weight': 33}), (165, 168, {'weight': 51}), (186, 165, {'weight': 133}), (150, 165, {'weight': 113}), (165, 163, {'weight': 90}), (163, 164, {'weight': 39}), (164, 167, {'weight': 70}), (161, 164, {'weight': 102}), (163, 160, {'weight': 109}), (154, 163, {'weight': 104}), (167, 162, {'weight': 164}), (314, 313, {'weight': 58})])
# Define nodes gps coords for plotting
nodes_coords = [[2.2562154, 48.9010192], [2.2552395, 48.9016349], [2.2561504, 48.9019072], [2.256213, 48.9023117], [2.2533483, 48.9022124], [2.2560583, 48.9031052], [2.2526502, 48.9024669], [2.2529865, 48.9025795], [2.2539978, 48.9033062], [2.254378, 48.90346], [2.2559377, 48.9039617], [2.2512099, 48.9031913], [2.2522591, 48.9034114], [2.2532497, 48.9037264], [2.2542639, 48.9040196], [2.255091, 48.9043102], [2.2558433, 48.904568], [2.2496848, 48.903958], [2.2506347, 48.9041945], [2.2516522, 48.9044511], [2.252662, 48.9046991], [2.2536487, 48.9049375], [2.2546109, 48.9051719], [2.2549951, 48.905281], [2.2557699, 48.9052503], [2.2560158, 48.9052059], [2.2562064, 48.9055009], [2.2558068, 48.9056019], [2.2557576, 48.9060545], [2.2555949, 48.9054919], [2.2550966, 48.9054932], [2.2556871, 48.9064052], [2.2550969, 48.9062517], [2.2533605, 48.9057877], [2.2531638, 48.9057675], [2.2527857, 48.9058181], [2.2527457, 48.9056766], [2.2511718, 48.9052887], [2.2497184, 48.9049283], [2.2507196, 48.9060941], [2.2496744, 48.9057506], [2.2495699, 48.9062557], [2.2513712, 48.9071609], [2.2526377, 48.9067325], [2.2532956, 48.9069103], [2.2522934, 48.907371], [2.2519614, 48.9079488], [2.2532156, 48.9082075], [2.2538981, 48.9083691], [2.2542177, 48.9078397], [2.2554719, 48.9081671], [2.2525332, 48.9090399], [2.2534123, 48.90925], [2.2530558, 48.9098965], [2.25506, 48.90965], [2.255269, 48.9089348], [2.2540012, 48.9107575], [2.2540107, 48.9110728], [2.2546194, 48.9112061], [2.2543603, 48.9116704], [2.2547269, 48.9108195], [2.2564689, 48.91308], [2.2573643, 48.9135046], [2.2578772, 48.9133618], [2.2581772, 48.9133246], [2.2578468, 48.9129075], [2.2575729, 48.9129246], [2.2566296, 48.9123618], [2.2565339, 48.9123161], [2.2568035, 48.9122761], [2.2577766, 48.9121258], [2.2582331, 48.9120772], [2.2585157, 48.9120658], [2.2582288, 48.9118886], [2.2586523, 48.9114136], [2.2577236, 48.9115026], [2.2576733, 48.9108796], [2.2566948, 48.9116761], [2.2570773, 48.9115161], [2.2570686, 48.9110647], [2.2571947, 48.9109475], [2.2570307, 48.9108279], [2.2568339, 48.9109761], [2.2562731, 48.9115704], [2.2562035, 48.9110504], [2.2555558, 48.9111047], [2.255594, 48.9108279], [2.2571386, 48.9104589], [2.2558423, 48.9102656], [2.2559971, 48.9097697], [2.2564539, 48.9091764], [2.2566337, 48.909212], [2.2574349, 48.9093881], [2.2575482, 48.9090306], [2.2569313, 48.9089723], [2.2566629, 48.9085482], [2.2576685, 48.9086267], [2.2580299, 48.9069557], [2.2580295, 48.9069669], [2.2581246, 48.9065475], [2.2582265, 48.9060973], [2.2595008, 48.9065057], [2.2576214, 48.9036038], [2.258192, 48.9030371], [2.2608928, 48.9040789], [2.2585407, 48.9027078], [2.2611337, 48.903683], [2.2590035, 48.9023244], [2.2615141, 48.9031121], [2.2593649, 48.9019535], [2.2599979, 48.9013088], [2.2612097, 48.9125436], [2.260279, 48.9009098], [2.2603523, 48.9014573], [2.2598385, 48.902108], [2.2674267, 48.9046065], [2.2631137, 48.9096459], [2.2598567, 48.9113223], [2.2591258, 48.9108471], [2.2616782, 48.9090033], [2.2631586, 48.9024119], [2.2628072, 48.90288], [2.2630198, 48.9038252], [2.2623468, 48.9050269], [2.2604064, 48.906693], [2.2614383, 48.9068184], [2.2623244, 48.9069216], [2.262792, 48.9069735], [2.2633002, 48.9052333], [2.2642648, 48.9053513], [2.2640068, 48.9046878], [2.2662902, 48.9055766], [2.2673633, 48.9041359], [2.2659173, 48.9040821], [2.2661083, 48.9037712], [2.2664721, 48.9034902], [2.2654807, 48.9031913], [2.264644, 48.9035978], [2.2632434, 48.9031973], [2.2633401, 48.9029807], [2.2635971, 48.9025452], [2.2637881, 48.9022197], [2.263517, 48.9014261], [2.2641711, 48.9016668], [2.2644167, 48.9013619], [2.2659446, 48.9014456], [2.2665042, 48.9017256], [2.2675759, 48.9021491], [2.2680658, 48.9022883], [2.2686981, 48.9025468], [2.2677508, 48.901677], [2.2680723, 48.9015171], [2.2685604, 48.9016879], [2.2692138, 48.9019026], [2.2675292, 48.9009815], [2.2682062, 48.9012765], [2.2685407, 48.9008935], [2.2697018, 48.9012894], [2.2699891, 48.9009349], [2.2710794, 48.9014187], [2.271658, 48.9016826], [2.2727461, 48.9028625], [2.2704859, 48.9023202], [2.2709616, 48.9024744], [2.2700183, 48.9030713], [2.2704252, 48.9032074], [2.270521, 48.9030343], [2.2697773, 48.9034972], [2.2712268, 48.9040632], [2.2715847, 48.9036559], [2.2724917, 48.903983], [2.2694406, 48.9040422], [2.2708196, 48.9045498], [2.2716365, 48.9048681], [2.2721175, 48.9050445], [2.2731382, 48.9054236], [2.274212, 48.9058739], [2.273371, 48.9051651], [2.274031, 48.905418], [2.2736921, 48.9048041], [2.2657764, 48.9028344], [2.2659614, 48.9026847], [2.2668887, 48.9032382], [2.2671231, 48.9031211], [2.2681017, 48.9032536], [2.2682582, 48.9033777], [2.2681951, 48.903597], [2.2680666, 48.9036817], [2.2677938, 48.9036361], [2.2673991, 48.9044948], [2.2675058, 48.9047469], [2.2684708, 48.9043492], [2.269155, 48.9045468], [2.2699618, 48.9048639], [2.2712455, 48.9052976], [2.2717233, 48.9054723], [2.272744, 48.9058413], [2.2738731, 48.9062439], [2.2745752, 48.9065469], [2.273713, 48.9064429], [2.2724316, 48.9062142], [2.2713243, 48.905923], [2.270826, 48.905793], [2.2693245, 48.9053044], [2.2691194, 48.9050719], [2.2689375, 48.9049471], [2.26872, 48.9049211], [2.2686013, 48.9049653], [2.2683007, 48.9049965], [2.2679048, 48.9055527], [2.2679447, 48.9058675], [2.2681243, 48.9060031], [2.2687562, 48.9062129], [2.2690023, 48.9059987], [2.2709032, 48.9062137], [2.2719633, 48.9064763], [2.2729046, 48.9066791], [2.2735216, 48.906783], [2.2733397, 48.9070794], [2.2695981, 48.9065442], [2.2703734, 48.9068299], [2.2713859, 48.9071938], [2.2721611, 48.9075058], [2.2726171, 48.9076682], [2.2743091, 48.908283], [2.2741894, 48.9090284], [2.2736517, 48.9089442], [2.2719104, 48.9084966], [2.2705944, 48.9077431], [2.2699098, 48.9073435], [2.2692446, 48.9069632], [2.2685289, 48.9065601], [2.2672885, 48.9059808], [2.2665336, 48.9061732], [2.2663407, 48.9064377], [2.2674448, 48.9068071], [2.2670989, 48.9072378], [2.2661648, 48.9069225], [2.2658755, 48.9073837], [2.2646118, 48.9072657], [2.2640264, 48.9071848], [2.2638568, 48.9071498], [2.2667601, 48.9076832], [2.2670129, 48.9077859], [2.267392, 48.9079083], [2.2677347, 48.9080464], [2.2682086, 48.9082068], [2.2685359, 48.9077867], [2.2692974, 48.9080752], [2.2702284, 48.9078244], [2.2704685, 48.9082794], [2.2730951, 48.9094461], [2.2743286, 48.9094969], [2.2739111, 48.9098387], [2.2743133, 48.9100381], [2.273694, 48.910819], [2.2731767, 48.910479], [2.272831, 48.9108932], [2.2727534, 48.9111977], [2.2727417, 48.911445], [2.2722382, 48.9116514], [2.2721749, 48.9109912], [2.2716607, 48.9110588], [2.2726719, 48.9098062], [2.2723442, 48.9101019], [2.2721867, 48.9102008], [2.2712374, 48.9092823], [2.2707741, 48.9091386], [2.2699911, 48.9088482], [2.269676, 48.9087415], [2.2695043, 48.9086658], [2.2691657, 48.9085576], [2.2690081, 48.9084865], [2.2686766, 48.9083768], [2.2684112, 48.9090522], [2.2682322, 48.9093352], [2.2679432, 48.9091509], [2.2673895, 48.9089533], [2.2666618, 48.9087454], [2.2657314, 48.9080209], [2.2652728, 48.9079841], [2.2642436, 48.9078518], [2.2641038, 48.9083003], [2.2645233, 48.9083297], [2.2654909, 48.9087377], [2.2650826, 48.9087598], [2.2648477, 48.9087818], [2.2646743, 48.9094215], [2.2653231, 48.9091972], [2.2663454, 48.9095824], [2.2675951, 48.9099931], [2.2679669, 48.9101127], [2.2684889, 48.9102634], [2.2689398, 48.9104142], [2.2691929, 48.9103674], [2.2704031, 48.9102634], [2.268133, 48.9106117], [2.266116, 48.9105857], [2.2654515, 48.9103986], [2.2655702, 48.910045], [2.2649532, 48.910279], [2.2643667, 48.9097486], [2.2642828, 48.9102412], [2.2647414, 48.9104875], [2.2653231, 48.9108514], [2.2658544, 48.9111381], [2.2659903, 48.9118675], [2.2666964, 48.911759], [2.2674906, 48.911633], [2.2681547, 48.911525], [2.2675522, 48.9122404], [2.2667854, 48.9125239], [2.2662651, 48.9127309], [2.2663336, 48.9132483], [2.2654573, 48.9138647], [2.2654093, 48.9134058], [2.2648206, 48.9134688], [2.264711, 48.9126499], [2.2655463, 48.9119615], [2.2651766, 48.9120335], [2.2650328, 48.912029], [2.2643334, 48.9109694], [2.264285, 48.9113353], [2.2635177, 48.9114514], [2.2644235, 48.912083], [2.2637089, 48.9121768], [2.2638868, 48.9131596], [2.2633592, 48.9136496], [2.2625931, 48.914287], [2.2616662, 48.9143797], [2.2628752, 48.9136949], [2.262475, 48.9137271], [2.2631873, 48.912824], [2.2626075, 48.9123158], [2.2622102, 48.912382], [2.261987, 48.9129747], [2.2615453, 48.9137895], [2.2597952, 48.9137838], [2.2618976, 48.9124362], [2.2616237, 48.9124767], [2.2613122, 48.9127984], [2.260833, 48.9127939], [2.2620653, 48.9118306], [2.2619261, 48.9115686], [2.2626904, 48.9111419], [2.2631283, 48.9103252], [2.2631915, 48.9096595], [2.264121, 48.9094753], [2.2638475, 48.9094188], [2.2639647, 48.9088819], [2.263549, 48.9089396], [2.2632847, 48.9089795], [2.2634289, 48.9085191], [2.2631399, 48.9084874], [2.2633015, 48.9079316], [2.2630093, 48.9078815], [2.2630574, 48.9076736], [2.2626826, 48.9077091], [2.2622566, 48.9077451], [2.2619322, 48.9077271], [2.2611271, 48.9075653], [2.2608027, 48.9076911], [2.2637496, 48.907593], [2.2635595, 48.9079466], [2.2637069, 48.9079082], [2.2622918, 48.9090983], [2.26203, 48.9092935], [2.2619401, 48.9090983], [2.2618736, 48.9091266], [2.2616389, 48.9100996], [2.2615685, 48.9104721], [2.2608181, 48.9105465], [2.2610331, 48.9112761], [2.2611308, 48.9116434], [2.261205, 48.9121854], [2.2614834, 48.9123754], [2.2606032, 48.9121648], [2.2606618, 48.9125244], [2.260271, 48.9121288], [2.2602827, 48.911443], [2.2598567, 48.9114096], [2.2598059, 48.9107931], [2.259802, 48.9106698], [2.2590789, 48.9107418], [2.2587819, 48.9128198], [2.2597746, 48.9126914], [2.2599935, 48.9132462], [2.2596456, 48.9134491], [2.2589304, 48.9143764], [2.2604515, 48.9144591], [2.2584497, 48.913765], [2.2579142, 48.9137599], [2.2586938, 48.9091509], [2.2581521, 48.9084752], [2.2588707, 48.9077377], [2.2590973, 48.9077195], [2.2594786, 48.9076941], [2.2597218, 48.9075633], [2.2593184, 48.9074253], [2.2600977, 48.9074689], [2.2608768, 48.9082346], [2.2606398, 48.9081958], [2.2600977, 48.9083957], [2.2598916, 48.9085872], [2.2593847, 48.9080465], [2.2595223, 48.9082404], [2.2592447, 48.9084144], [2.2594851, 48.9084943], [2.2595212, 48.9086655], [2.2604166, 48.9091216], [2.2601929, 48.9091362], [2.2599347, 48.9088928], [2.260161, 48.908944], [2.2603271, 48.9088699], [2.2747081, 48.9069078], [2.2584055, 48.9093463], [2.2557686, 48.912472], [2.2590996, 48.9081554]]
# Define path we want to aniate as a list of nodes to travel through
path = [102, 125, 126, 124, 129, 130, 131, 123, 109, 123, 124, 126, 125, 400, 398, 397, 405, 406, 403, 404, 412, 413, 414, 402, 403, 406, 408, 409, 408, 407, 418, 405, 418, 394, 393, 409, 412, 413, 411, 416, 411, 410, 401, 410, 120, 369, 368, 359, 368, 366, 358, 357, 356, 355, 354, 353, 364, 365, 364, 353, 354, 355, 356, 357, 358, 357, 363, 242, 241, 351, 350, 349, 348, 349, 350, 287, 288, 287, 286, 281, 286, 285, 280, 281, 282, 240, 282, 283, 284, 283, 350, 351, 352, 351, 241, 240, 239, 238, 235, 234, 233, 211, 210, 209, 191, 116, 190, 188, 190, 116, 192, 193, 194, 205, 194, 195, 174, 173, 169, 168, 165, 163, 160, 163, 165, 166, 165, 186, 165, 150, 149, 153, 152, 153, 154, 163, 164, 161, 164, 167, 162, 167, 166, 170, 171, 170, 169, 168, 172, 173, 174, 175, 176, 178, 180, 178, 179, 178, 176, 177, 198, 200, 199, 200, 198, 197, 176, 175, 196, 197, 201, 200, 201, 202, 203, 202, 196, 195, 203, 204, 205, 206, 207, 192, 116, 191, 209, 210, 211, 212, 236, 235, 238, 237, 243, 237, 236, 212, 213, 214, 215, 216, 215, 221, 220, 231, 248, 231, 220, 213, 232, 231, 232, 245, 232, 213, 212, 211, 210, 209, 208, 207, 206, 193, 172, 187, 188, 189, 188, 187, 186, 187, 186, 185, 150, 154, 158, 159, 158, 157, 156, 155, 156, 152, 151, 148, 147, 146, 147, 148, 149, 148, 184, 185, 189, 183, 184, 182, 181, 182, 144, 145, 144, 143, 144, 142, 141, 142, 181, 183, 136, 137, 136, 135, 134, 135, 140, 139, 138, 139, 140, 122, 121, 114, 111, 113, 111, 110, 115, 114, 121, 141, 140, 135, 133, 132, 239, 243, 244, 279, 278, 246, 278, 277, 291, 277, 247, 248, 249, 273, 249, 230, 231, 230, 221, 222, 216, 217, 218, 415, 218, 219, 218, 217, 223, 222, 223, 224, 223, 229, 230, 229, 228, 224, 225, 227, 228, 229, 250, 271, 272, 273, 274, 275, 274, 247, 246, 245, 244, 279, 290, 289, 290, 291, 292, 276, 292, 293, 294, 293, 272, 271, 270, 295, 270, 269, 251, 269, 268, 267, 228, 227, 226, 227, 252, 264, 252, 254, 253, 254, 255, 254, 257, 265, 257, 258, 259, 256, 259, 260, 261, 262, 263, 262, 266, 265, 264, 267, 268, 296, 266, 296, 295, 294, 297, 298, 299, 300, 299, 301, 303, 302, 303, 346, 351, 352, 366, 352, 117, 347, 346, 345, 346, 371, 370, 371, 372, 367, 369, 120, 401, 402, 362, 361, 126, 127, 360, 127, 128, 242, 130, 132, 239, 280, 285, 289, 301, 304, 321, 320, 319, 305, 319, 307, 308, 312, 308, 309, 311, 309, 310, 309, 308, 307, 306, 307, 319, 320, 316, 314, 313, 314, 316, 317, 318, 317, 328, 333, 328, 331, 334, 335, 332, 331, 334, 326, 327, 326, 324, 326, 325, 323, 322, 323, 325, 320, 316, 315, 329, 330, 390, 330, 337, 338, 337, 341, 337, 332, 335, 339, 336, 339, 340, 341, 342, 387, 388, 391, 392, 391, 388, 389, 388, 387, 342, 112, 342, 341, 340, 376, 112, 375, 374, 344, 374, 373, 372, 373, 345, 343, 375, 343, 376, 375, 378, 386, 378, 377, 379, 380, 118, 381, 118, 386, 385, 66, 64, 63, 64, 65, 64, 66, 67, 62, 67, 68, 70, 78, 79, 80, 81, 80, 83, 85, 84, 78, 70, 71, 72, 74, 72, 73, 72, 71, 66, 385, 75, 76, 75, 381, 382, 383, 382, 119, 384, 77, 81, 82, 83, 85, 86, 87, 86, 59, 58, 59, 57, 59, 60, 59, 61, 87, 82, 88, 93, 416, 393, 394, 97, 94, 95, 92, 93, 88, 89, 90, 55, 61, 59, 69, 417, 69, 68, 70, 71, 76, 77, 384, 383, 372, 367, 366, 358, 359, 360, 361, 362, 400, 399, 398, 397, 396, 399, 396, 395, 98, 99, 98, 97, 94, 93, 92, 91, 96, 91, 56, 51, 56, 55, 53, 54, 53, 52, 53, 49, 48, 49, 50, 46, 47, 46, 43, 46, 44, 45, 44, 35, 36, 40, 42, 40, 41, 40, 38, 39, 38, 37, 36, 37, 38, 20, 19, 18, 19, 12, 13, 20, 21, 14, 21, 22, 35, 34, 33, 50, 51, 32, 99, 100, 101, 27, 28, 29, 28, 30, 31, 30, 25, 26, 27, 26, 103, 26, 25, 17, 11, 17, 16, 23, 22, 15, 10, 9, 10, 11, 6, 5, 8, 7, 8, 13, 14, 15, 16, 23, 24, 31, 34, 33, 32, 29, 100, 101, 102, 105, 107, 106, 107, 109, 108, 110, 108, 106, 104, 103, 6, 4, 3, 2, 3, 1, 3, 4, 104, 105, 102]
#path = [102, 125, 126, 127, 128, 242] # simpler path for testing


# SIMPLE graph definition (for testing)
#G = nx.Graph()
#G.add_edges_from([(1,2, {'weight': 1}), (3,4, {'weight': 1}), (2,5, {'weight': 1}), (4,5, {'weight': 1}), (6,7, {'weight': 1}), (8,9, {'weight': 1}), (4,7, {'weight': 1}), (1,7, {'weight': 1}), (3,5, {'weight': 1}), (2,7, {'weight': 1}), (5,8, {'weight': 1}), (2,9, {'weight': 1}), (5,1, {'weight': 1})])
#nodes_coords = [[1,2], [1,5], [6,5], [3,5], [4,1], [6,2], [2,8], [3,3], [2,5]]
#path = [6, 7, 2, 9, 8, 5, 2, 1, 5, 3, 4, 7, 1, 5, 4]


# Define the list of edges and store nodes coords as a parameter
edges = [e for e in G.edges]
for u, v in enumerate(nodes_coords):
    G.nodes[u+1]['pos'] = (v[0], v[1])


# Draw the graph before animating the path
edges_color = ["#d9d9d9" for u in G.edges]
nodes_color = ["#a7a7a7" for u in G.nodes]
nx.draw(G, nx.get_node_attributes(G, 'pos'), node_size=140, font_size=6, width=2, node_color=nodes_color, edge_color=edges_color)
#nx.draw(G, nx.get_node_attributes(G, 'pos'), with_labels=True, node_size=140, font_size=6, width=2, node_color=nodes_color, edge_color=edges_color) # with nodes labels -> much slower
fig = plt.gcf()


# Animation function
def animate(frame):
    global nodes_color, edges_color
    # Color in red the currently visited path node
    nodes_color = ["red" if (u == path[frame]) else "#a7a7a7" for u in G.nodes]
    # Set the new visited edge of the path in green (cumulative coloring of what has been visited)
    if (frame > 0):
        try:
            edges_color[edges.index((min(path[frame-1],path[frame]), max(path[frame-1],path[frame])))] = "green"
        except ValueError:
            edges_color[edges.index((max(path[frame-1],path[frame]), min(path[frame-1],path[frame])))] = "green"
    else:
        time.sleep(3) # to allow me to increase the the matplotlib graph window before starting to draw the path
        edges_color = ["#d9d9d9" for u in G.edges] # all edges in grey before showing the circuit in green
    nx.draw(G, nx.get_node_attributes(G, 'pos'), node_size=140, font_size=6, width=2, node_color=nodes_color, edge_color=edges_color)
    #plt.savefig("graph-" + str(frame) + ".jpg", quality=100, optimize=True,) # save image of the current graph - for testing
    #nx.draw(G, nx.get_node_attributes(G, 'pos'), with_labels=True, node_size=140, font_size=6, width=2, node_color=nodes_color, edge_color=edges_color) # with nodes labels -> much slower


anim = animation.FuncAnimation(fig, animate, frames=len(path), interval=200, blit=False, repeat=False, cache_frame_data=False)
plt.show()

Если вы хотите протестировать более простой график / путь, вы можете прокомментировать первый блок «Определить график» и раскомментировать следующий «Определение ПРОСТОГО графа (для тестирования)».

Вот как гладко это выглядит с этим небольшим тестовым графиком и путем:
введите описание изображения здесь

Для большого графика / пути я не могу сохранить его, чтобы показать вам.

0

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *