Skip to content
This repository was archived by the owner on Dec 2, 2025. It is now read-only.

Commit 9fd029b

Browse files
refactor: update manim examples to latest syntax
- Replace deprecated GraphScene with Axes API - Update background color to use self.camera.background_color - Replace GrowFromCenter with Create for consistency - Add new examples: WriteEquation, EquationSteps, MovingCamera, MoveObjectsTogether, TracePath - Simplify Graph example to use modern Axes approach - Remove duplicate imports and config.background_color usage - Match examples with codecut_articles/manim_mathematical_animations.qmd
1 parent 06ee55b commit 9fd029b

2 files changed

Lines changed: 112 additions & 50 deletions

File tree

visualization/manim_exp/more.py

Lines changed: 87 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from manim import *
22

3+
34
class MovingFrame(Scene):
45
def construct(self):
56
# Write equations
@@ -18,7 +19,7 @@ def construct(self):
1819
self.wait()
1920
# replace frame 1 with frame 2
2021
self.play(ReplacementTransform(framebox1, framebox2))
21-
22+
2223
self.wait()
2324

2425

@@ -59,40 +60,26 @@ def construct(self):
5960
self.wait(0.3)
6061
self.play(self.camera.frame.animate.move_to(equation[2]).set(width=equation[2].width*2))
6162

62-
class Graph(GraphScene):
63-
def __init__(self, **kwargs):
64-
GraphScene.__init__(
65-
self,
66-
x_min=-3.5,
67-
x_max=3.5,
68-
y_min=-5,
69-
y_max=5,
70-
graph_origin=ORIGIN,
71-
axes_color=BLUE,
72-
x_labeled_nums=range(-4, 4, 2), # x tickers
73-
y_labeled_nums=range(-5, 5, 2), # y tickers
74-
**kwargs
75-
)
76-
63+
class Graph(Scene):
7764
def construct(self):
78-
self.setup_axes(animate=True)
65+
axes = Axes(
66+
x_range=[-3, 3, 1],
67+
y_range=[-5, 5, 1],
68+
x_length=6,
69+
y_length=6,
70+
)
7971

80-
# Draw graph
81-
func_graph_cube = self.get_graph(lambda x: x**3, RED)
82-
func_graph_ncube = self.get_graph(lambda x: -x**3, GREEN)
72+
# Add labels
73+
axes_labels = axes.get_axis_labels(x_label="x", y_label="f(x)")
8374

84-
# Create labels
85-
graph_lab = self.get_graph_label(func_graph_cube, label="x^3")
86-
graph_lab2 = self.get_graph_label(func_graph_ncube, label="-x^3", x_val=-3)
75+
# Create function graphs
76+
graph = axes.plot(lambda x: x**2, color=BLUE)
77+
graph_label = axes.get_graph_label(graph, label="x^2")
8778

88-
# Create a vertical line
89-
vert_line = self.get_vertical_line_to_graph(1.5, func_graph_cube, color=YELLOW)
90-
label_coord = self.input_to_graph_point(1.5, func_graph_cube)
91-
text = MathTex(r"x=1.5")
92-
text.next_to(label_coord)
93-
94-
self.add(func_graph_cube, func_graph_ncube, graph_lab, graph_lab2, vert_line, text)
95-
self.wait(3)
79+
self.add(axes, axes_labels)
80+
self.play(Create(graph))
81+
self.play(Write(graph_label))
82+
self.wait()
9683

9784
class GroupCircles(Scene):
9885
def construct(self):
@@ -101,19 +88,19 @@ def construct(self):
10188
circle_green = Circle(color=GREEN)
10289
circle_blue = Circle(color=BLUE)
10390
circle_red = Circle(color=RED)
104-
91+
10592
# Set initial positions
10693
circle_green.shift(LEFT)
10794
circle_blue.shift(RIGHT)
108-
95+
10996
# Create 2 different groups
11097
gr = VGroup(circle_green, circle_red)
11198
gr2 = VGroup(circle_blue)
11299
self.add(gr, gr2) # add two groups to the scene
113100
self.wait()
114101

115102
self.play((gr + gr2).animate.shift(DOWN)) # shift 2 groups down
116-
103+
117104
self.play(gr.animate.shift(RIGHT)) # move only 1 group
118105
self.play(gr.animate.shift(UP))
119106

@@ -138,3 +125,69 @@ def construct(self):
138125
# Shift the circle to 8*RIGHT
139126
self.play(rolling_circle.animate.shift(8*RIGHT), run_time=4, rate_func=linear)
140127

128+
class WriteEquation(Scene):
129+
def construct(self):
130+
equation = MathTex(r"e^{i\pi} + 1 = 0")
131+
132+
self.play(Write(equation))
133+
self.wait()
134+
135+
class EquationSteps(Scene):
136+
def construct(self):
137+
step1 = MathTex(r"2x + 5 = 13")
138+
step2 = MathTex(r"2x = 8")
139+
step3 = MathTex(r"x = 4")
140+
141+
self.play(Write(step1))
142+
self.wait()
143+
self.play(Transform(step1, step2))
144+
self.wait()
145+
self.play(Transform(step1, step3))
146+
self.wait()
147+
148+
class MovingCamera(MovingCameraScene):
149+
def construct(self):
150+
equation = MathTex(
151+
r"\frac{d}{dx}(x^2) = 2x"
152+
)
153+
154+
self.play(Write(equation))
155+
self.wait()
156+
157+
# Zoom in on the derivative
158+
self.play(
159+
self.camera.frame.animate.scale(0.5).move_to(equation[0])
160+
)
161+
self.wait()
162+
163+
class MoveObjectsTogether(Scene):
164+
def construct(self):
165+
square = Square(color=BLUE)
166+
circle = Circle(color=RED)
167+
168+
# Group objects
169+
group = VGroup(square, circle)
170+
group.arrange(RIGHT, buff=1)
171+
172+
self.play(Create(group))
173+
self.wait()
174+
175+
# Move the entire group
176+
self.play(group.animate.shift(UP * 2))
177+
self.wait()
178+
179+
class TracePath(Scene):
180+
def construct(self):
181+
dot = Dot(color=RED)
182+
183+
# Create traced path
184+
path = TracedPath(dot.get_center, stroke_color=BLUE, stroke_width=4)
185+
self.add(path, dot)
186+
187+
# Move the dot in a circular pattern
188+
self.play(
189+
MoveAlongPath(dot, Circle(radius=2)),
190+
rate_func=linear,
191+
run_time=4
192+
)
193+
self.wait()

visualization/manim_exp/start.py

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
1-
from manim import *
1+
from manim import *
22

3-
config.background_color = DARK_GRAY
43

5-
from manim import *
4+
class GrowingSquare(Scene):
5+
def construct(self):
6+
square = Square(color=BLUE, fill_opacity=0.5)
7+
self.play(GrowFromCenter(square))
8+
self.wait()
69

7-
class PointMovingOnShapes(Scene):
10+
class SquareToCircle(Scene):
811
def construct(self):
9-
10-
# Create a square
11-
square = Square(color=BLUE)
12-
square.flip(RIGHT)
13-
square.rotate(-3 * TAU / 8)
14-
15-
# Create a circle
1612
circle = Circle()
1713
circle.set_fill(PINK, opacity=0.5)
18-
19-
# Create animations
20-
self.play(GrowFromCenter(square))
21-
self.play(Transform(square, circle))
22-
14+
15+
square = Square()
16+
square.rotate(PI / 4)
17+
18+
self.play(Create(square))
19+
self.play(Transform(square, circle))
20+
self.play(FadeOut(square))
21+
22+
class CustomBackground(Scene):
23+
def construct(self):
24+
self.camera.background_color = WHITE
25+
26+
square = Square(color=BLUE, fill_opacity=0.5)
27+
circle = Circle(color=RED, fill_opacity=0.5)
28+
29+
self.play(Create(square))
30+
self.wait()
31+
self.play(Transform(square, circle))
2332
self.wait()
2433

2534

0 commit comments

Comments
 (0)