5

Currently trying to display the edge weights from a network x graph in Manim, here is my current code:

from manim import *

import networkx as nx

class Run(Scene):
    def construct(self):
        G = nx.Graph()

        G.add_nodes_from([node for node in "abcdef"])
        G.add_edge("a","b", weight=17)
        G.add_edge("a","c", weight=12)
        G.add_edge("a","d", weight=8)
        G.add_edge("b","c", weight=1)
        G.add_edge("b","e", weight=6)
        G.add_edge("c","d", weight=7)
        G.add_edge("c","f", weight=11)
        G.add_edge("d","f", weight=15)
        G.add_edge("e","f", weight=5)

        edge_labels = nx.get_edge_attributes(G, "weight")
        pos = nx.spring_layout(G)
        nx.draw_networkx_edges(G, pos)
        nx.draw_networkx_nodes(G, pos)
        nx.draw_networkx_labels(G,pos)
        nx.draw_networkx_edge_labels(G, pos, edge_labels)

        self.play(Create(Graph(list(G.nodes), list(G.edges), layout="spring", layout_scale=3, root_vertex="None", edge_config={("a","b"): {"stroke_color": RED}}))))
        self.wait()

Here is the current output: NX Graph with No Weighted Edges

A huge part of animating this algorithm will be showing the weighted edge values. As you can see, I've coded them into my graph but they do not appear when the graph is made in the video. Maybe I overlooked something in the documentation, but does anyone have any suggestions on how to display edge weights in an NX graph in Manim?

MrFreeze2017
  • 79
  • 1
  • 7

1 Answers1

0

There isn't a built-in way of doing this in Manim, but would be a good enhancement request. I checked on GitHub and didn't see any issue open for this enhancement yet.

Alex
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 09 '23 at 10:29