pythonmanim

Manim set_opacity doesn't work in the expected manner


When running the following code and generating the output animation, I expect the orange graph to slightly fade to opacity of 0.3. Unfortunately it does not work as expected. The area under the plot becomes colored which is not what I want. I have attached an image of the final output to the bottom of the question. Is this a bug or am I doing something wrong?

import numpy as np
from manim import *

class LinearRegression(Scene):
    def construct(self):
        plot = VGroup()
        ax = Axes(
            x_range=[0, np.pi + 0.1, 1],
            y_range=[0, 1.1, 0.2],
            tips=True,
            axis_config={"include_numbers": True},
            x_length=7,
            y_length=5,
        ).add_coordinates()
        
        plot.add(ax)

        x = np.array([0., 0.16534698, 0.33069396, 0.49604095, 0.66138793,
                       0.82673491, 0.99208189, 1.15742887, 1.32277585, 1.48812284,
                       1.65346982, 1.8188168, 1.98416378, 2.14951076, 2.31485774,
                       2.48020473, 2.64555171, 2.81089869, 2.97624567, 3.14159265])

        y = np.array([0.01313289,  0.9531357,  0.27303831,  0.56426336,  0.69446266,
                         0.79658221,  0.72262249,  1.0565153,  0.90835232,  1.01992807,
                         1.11962864,  0.85117504,  1.01662771,  0.91446878,  0.67722208,
                         0.6222458,  0.511015,  0.39366855,  0.18442382, 0.03879388])

        plr = np.poly1d(np.polyfit(x, y, 50))

        graph = ax.plot(plr, x_range=[0, np.pi, 0.165346982],
                           use_smoothing=False, color=ORANGE)

        self.play(FadeIn(ax))
        self.wait()
        self.play(Create(graph))
        self.wait()
        self.play(graph.animate.set_opacity(0.3))
        self.wait(5)

enter image description here


Solution

  • I can only guess that graph has many objects - line, background - and when you set opacity then it changes it for all objects. And it changes line from 1.0 to 0.3 and background from 0.0 to 0.3

    But I see it works correctly for FadeOut(graph) (but it changes opacity to 0.0)

    Finally I found this:

    self.play( graph.animate.fade(0.3) )
    

    BTW:

    I'm not sure if fade(0.3) means opacity(0.3) or opacity(1-0.3).
    Sometimes fade doesn't give me smooth animation.


    EDIT:

    I found other method which gives me smooth animation:

    self.play( graph.animate.set_stroke(ORANGE, opacity=0.3) )
    

    I tried also set_stroke_opacity(0.3) but this doesn't change opacity (but doesn't raise error).

    If you want to change only background then use

    self.play( graph.animate.set_fill(ORANGE, opacity=0.3) )
    

    VMobject - Manim Community v0.18.0.post0