Giter Club home page Giter Club logo

Comments (2)

behackl avatar behackl commented on June 8, 2024

That's a fun one -- and actually not really the fault of Graph; it's a consequence of the behavior of dict.fromkeys. You can check that this version (after slightly increasing the vertex radius) renders correctly:

import manim as mn
class KonigsbergBridgeNotWorking(mn.Scene):
    def construct(self):
        self.camera.background_color = mn.DARKER_GREY

        title_text = mn.Text(text="KonigsbergBridgeNotWorking", color=mn.WHITE).shift(
            mn.UP * 3.5
        )

        vertices: list = [0, 1, 2, 3]
        edges: list = [(0, 1), (0, 2), (0, 3), (1, 0), (1, 2), (2, 3), (3, 0)]

        graph = mn.Graph(
            vertices=vertices,
            edges=edges,
            labels=True,
            vertex_config={v: {"color": mn.PINK, "radius": 0.3} for v in vertices},
            edge_type=mn.Line,
            edge_config={e: {"path_arc": mn.PI / 4, "color": mn.WHITE} for e in edges},
        )

        self.play(mn.AddTextLetterByLetter(title_text))

        self.play(mn.Create(graph))

        self.wait(3)

The issue with dict.fromkeys is that if you use a dictionary (as a complex data type) as a value, the separate values will actually just be shallow copies of each other; they reference the same dict under the hood. Here is a plain example illustrating this behavior:

>>> conf = {'x': 1, 'y': 2}
>>> dct = dict.fromkeys([7, 8], conf); dct
{7: {'x': 1, 'y': 2}, 8: {'x': 1, 'y': 2}}
>>> dct[7]['x'] = 42
>>> dct
{7: {'x': 42, 'y': 2}, 8: {'x': 42, 'y': 2}}

The Graph mobject doesn't have any safeguards to fix this internally, the identical config dicts appear to mess things up in a weird way. You can check that by printing the value of graph._vertex_config; its entries will all be the exact same dict.

from manim.

seanpden avatar seanpden commented on June 8, 2024

Wow, just looked at the docs and your example - you're totally right. You learn something new every day. With the fault being dict.fromkeys passing a reference to the same instance, I'd wager that it's not worth it to put a check in the Graph constuctor.

Thanks for the help! You think this one can be closed out?

from manim.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.