Maths Animations with Pymations
Pymations ("Python animations") makes the kind of animation used in maths explainer videos: axes are drawn, a curve grows along them, an equation writes itself, one shape turns into another. You say what should appear and in what order, and Pymations works out the movement.
Who this is for
A student of maths or physics at school, college or university, a teacher making an explanation for a class, or anyone who has watched a maths video and wanted to make one. You should know a little Python: variables and calling a function. Knowing how maths is written in LaTeX helps for equations, and you can start without it.
What you need
- Circuitry in a web browser, in the desktop app, or on an iPad or a phone.
- Nothing to install.
Your first program
Make a Python file ( → Code → Python) and type or paste this:
from pymations import *
title = Text("A parabola").to_edge(UP)
axes = Axes(x_range=[-3, 3, 1], y_range=[0, 9, 3], x_length=8, y_length=5)
parabola = axes.plot(lambda x: x ** 2, color=BLUE)
label = axes.get_graph_label(parabola, "y = x^2", x_val=2)
dot = Dot(axes.c2p(-3, 9), color=YELLOW)
play(Write(title))
play(Create(axes))
play(Create(parabola), Write(label))
play(GrowFromCenter(dot))
play(MoveAlongPath(dot, parabola), run_time=3)
wait()
Press Run in the footer. The first line, from pymations import *, is what makes the file an animation.
What you should see
The title is written at the top. The two axes are drawn, then the curve grows from left to right while its label is written beside it. A yellow dot appears at the top left of the curve and slides down it, through the bottom and up the other side.
- The first five lines only make things. Nothing is on screen yet.
- Each
play(...)is one step of the animation. Two things inside oneplayhappen together. axes.plot(lambda x: x ** 2)draws a Python function as a curve.lambda x: x ** 2is a short way to write a function that takes x and gives back x squared.wait()holds the last picture for a second.
Move the pointer over the preview, or tap it, and a timeline appears: drag along it to go to any moment, forwards or backwards.
Three things to try
- Another curve. Change
x ** 2to9 - x ** 2, and the label to"y = 9 - x^2". The dot starts at the bottom left now, so changeaxes.c2p(-3, 9)toaxes.c2p(-3, 0). - Slower. Change
run_time=3torun_time=8. - Colours. Change
BLUEtoGREENandYELLOWtoRED. See Colours.
Make a video of it
The button in the footer records the animation to an MP4 file. See Recording videos.
Where to go next
- Your first animation: the same ideas, one step at a time.
- Text and maths and Graphs.
- Tutorials: an equation explainer, plotting a graph and Pythagoras by rearrangement.
- Running Manim scripts: a script from a Manim tutorial can be pasted in and run.
- Data and science: the next path.