[Manim] Manim Examples

A Ydobon
5 min readMay 9, 2020

--

Note that the examples are written in the decreasing order of difficulty. So if you cannot understand the first, you may go down on the next example. Because I am in the process of learning, I write the most recent example on the top.

Graph stick to the axes

Now the graph is ON the axes. Use get_graph .

Graph with axes in a stupid way

It looks like a graph on axes, but it is not related to the axes. It is a separate object.

Graph without axes

Grouping and moving together

VGroup is for grouping objects. Then we can play some movements on the group.

Add a circle

The default position is at the center of a screen. A circle is just an example. There are lots of different shapes you may try.

Minimal

This is a minimal manim example. It just waits for one second (default value) on the black screen.

I don’t remember what I did from here.

from manimlib.imports import *class a20200509_10(Scene):
def construct(self):

fx = lambda x: x.get_value()**2

x_value = ValueTracker(0)
fx_value = ValueTracker(fx(x_value))
x_tex = Integer(x_value.get_value()).add_updater(lambda v: v.set_value(x_value.get_value()))
dot = Dot(radious = x_value.get_value()).add_updater(lambda x: x.move_to(0.1*x_value.get_value()))

self.add(x_tex)
self.add(dot)

self.play(
x_value.set_value,30,
rate_func=linear,
run_time=10
)
self.wait()
a20200509_10.py

a20200509_08.py

from manimlib.imports import *class a20200509_08(Scene):
def construct(self):
str = r"""
C = {1 \over r} \sum^{n}_{j=0} {{n}\choose{j}} p^{j} (1-p)^{n-j}
\max [ u^{j} d^{n-j} S - K , 0 ]
"""
str = str.split()
f_1 = TexMobject(*str)
f_1.scale(0.7)
self.play(Write(f_1))
self.wait()
str = r"""
= {1 \over r} \sum^{n}_{j=a} {{n}\choose{j}} p^{j} (1-p)^{n-j}
( u^{j} d^{n-j} S - K )
"""
str = str.split()
f_2 = TexMobject(*str)
f_2.scale(0.7)
self.play(ApplyMethod(f_1.shift, UP*1.2))
self.play(Write(f_2))
self.wait()
str = r"""
= S {1 \over r} \sum^{n}_{j=a} {{n}\choose{j}} p^{j} (1-p)^{n-j} u^{j} d^{n-j}
- K {1 \over r} \sum^{n}_{j=a} {{n}\choose{j}} p^{j} (1-p)^{n-j}
"""
str = str.split()
f_3 = TexMobject(*str)
f_3.scale(0.7)
self.play(ApplyMethod(f_1.shift, UP*1.2), ApplyMethod(f_2.shift, UP*1.2))
self.play(Write(f_3))
self.wait()
str = r"""
= S {1 \over r} \sum^{n}_{j=a} {{n}\choose{j}} [ p u ]^{j} [ (1-p) d ]^{n-j}
- K {1 \over r} \sum^{n}_{j=a} {{n}\choose{j}} p^{j} (1-p)^{n-j}
"""
str = str.split()
f_4 = TexMobject(*str)
f_4.scale(0.7)
self.play(Write(f_4.shift(DOWN*1.2)))
self.wait()
str = r"""
= S \sum^{n}_{j=a} {{n}\choose{j}}
\Big[ {p u \over r} \Big]^{j} \Big[ {(1-p) d \over r} \Big]^{n-j}
- K {1 \over r} \sum^{n}_{j=a} {{n}\choose{j}} p^{j} (1-p)^{n-j}
"""
str = str.split()
f_5 = TexMobject(*str)
f_5.scale(0.7)
self.play(Write(f_5.shift(DOWN*2.4)))
self.wait()
a20200509_08.py

a20200507_10.py

from manimlib.imports import *class a20200507_10(GraphScene):

CONFIG = {
"y_max" : 1,
"x_max" : 1,
"axes_color" : BLUE,
}
def construct(self):self.setup_axes(animate=True)g = lambda x : 4 * x * (1-x)graph = self.get_graph(
g,
color = GREEN
)
self.play(
ShowCreation(graph),
)
self.wait(1)
h = lambda x : xgraph = self.get_graph(
h,
color = RED
)
self.play(
ShowCreation(graph),
)
self.wait(1)
pt = Dot()
# pt.move_to(self.coords_to_point(0.5, 0.3))
# self.play(
# ShowCreation(pt),
# )
# #pt.move_to(self.coords_to_point(0.5, 0.5))
# self.play(
# ApplyMethod(pt.move_to, self.coords_to_point(0.5, 0.5) )
# )
x = [0.1, 0.2, ]
x = 0.3
for i in range(30):

self.play(ApplyMethod(pt.move_to, self.coords_to_point(x, g(x))))
self.play(ApplyMethod(pt.move_to, self.coords_to_point(g(x,), g(x))))
x = g(x)
self.graph_origin = RIGHT*2 + UP*2
self.setup_axes(animate=True)
g = lambda x : 4 * x * (1-x)graph = self.get_graph(
g,
color = GREEN
)
self.play(
ShowCreation(graph),
)
self.wait(1)
x = [0.1, 0.2, ]
x = 0.3
for i in range(30):

self.play(ApplyMethod(pt.move_to, self.coords_to_point(x, g(x))))
self.play(ApplyMethod(pt.move_to, self.coords_to_point(g(x,), g(x))))
x = g(x)
a20200507_10.py

a20200509_12.py

from manimlib.imports import *
from scipy.stats import binom
class a20200509_12(GraphScene):

CONFIG = {
"y_min" : 0,
"y_max" : 0.3,
"x_min" : 0,
"x_max" : 31,
"y_axis_label": "$\\mathbb{P} (X=x)$",
}
def construct(self):

self.setup_axes(animate=True,)
N = 10
p = 0.6
v_1 = self.plot_things(4,p)
self.add(v_1)

for N in range(5,31):
v = self.plot_things(N, p)
self.play(Transform(v_1, v))
self.add(v_1)
self.wait()
def plot_things(self, N, p):string = "N = {}, p = {}".format(N,p)
string = string.split()
f_1 = TexMobject(*string)
f_1.move_to(UP*3+RIGHT)
things = VGroup()for i in range(N+1):
thing = Dot()
thing.move_to(self.coords_to_point(i, binom.pmf(i, n=N, p=p)))
things.add(thing)

return VGroup(f_1, things)
a20200509_12.py

a20200510_05.py

2.5 is related to graph orientation.

from manimlib.imports import *
from scipy.stats import binom
class a20200510_05(GraphScene):

CONFIG = {
"y_min" : 0,
"y_max" : 0.3,
"x_min" : 0,
"x_max" : 31,
"y_axis_label": "$\\mathbb{P} (X=x)$",
}
def construct(self):

self.setup_axes(animate=True,)
N = 10
p = 0.2
v_1 = self.plot_things(4,p)
self.add(v_1)

for N in range(5,31):
v = self.plot_things(N, p)
self.play(Transform(v_1, v))
self.add(v_1)
self.wait()
def plot_things(self, N, p):string = "N = {}, p = {}".format(N,p)
string = string.split()
f_1 = TexMobject(*string)
f_1.move_to(UP*3+RIGHT)
things = VGroup()for i in range(N+1):
thing = Dot()
line = Line(start=[0,0.01,0], end=[0,0,0])
line.set_length(self.coords_to_point(i, binom.pmf(i, n=N, p=p))[1] + 2.5)
thing.move_to(self.coords_to_point(i, binom.pmf(i, n=N, p=p)))
line.move_to(self.coords_to_point(i, binom.pmf(i, n=N, p=p)/2))
things.add(thing, line)

return VGroup(f_1, things)
a20200510_05.py

Next

--

--

A Ydobon
A Ydobon

No responses yet