Creating animations
The primary method for creating animations ismake_animation(), which generates both a static PNG and an animated GIF.
Basic usage
fibonacci.gif- Animated visualization showing the tree being builtfibonacci.png- Static image of the complete recursion tree
Animation methods
The Visualiser class provides three class methods for generating output:make_animation()
Generates both an animated GIF and a static PNG image.
The name of the output GIF file. The PNG file will use the same base name.
The frame rate for the animation, specified as frames per second (fps).
- Lower values (1-2) = slower animation
- Medium values (3-5) = moderate speed
- Higher values (6+) = faster animation
The
delay parameter is passed to imageio.mimsave() as the fps parameter, controlling animation speed.write_image()
Generates only a static PNG image of the complete recursion tree.
The name of the output PNG file.
write_gif()
Generates only an animated GIF. This is typically called internally by make_animation(), but can be used directly if you’ve already generated frames.
The name of the output GIF file.
The frame rate (frames per second) for the animation.
How animations work
The animation generation process involves several steps:Recursion tree construction
As your decorated function executes, the visualiser tracks every function call and builds an internal representation of the recursion tree.
Frame generation
The
make_frames() method creates individual PNG images for each step of the recursion, stored in a temporary frames/ directory.Each frame shows the tree at a specific point during execution, with completed nodes fully visible and pending nodes invisible.GIF compilation
The individual frames are combined into an animated GIF using the
imageio library, with the specified frame rate.Animation examples
Example 1: Fibonacci with custom styling
Example 2: Factorial with slow animation
Example 3: String combinations
Example 4: Coin change problem
Choosing the right output method
- Use make_animation() when...
- Use write_image() when...
- You want to show the recursion process step by step
- Creating educational content
- Demonstrating algorithm behavior
- You need both static and animated outputs
Performance considerations
Animations can take significant time and disk space for deep recursion trees:Frame generation
Each frame is a separate PNG image. For a function with N recursive calls, N frames will be generated.Optimization tips
Test with small inputs first
Start with small input values to verify your visualization settings before generating large animations.
Use write_image() for large inputs
If you have a deep recursion tree but don’t need animation, use
write_image() to save time and space.Adjust delay for file size
Higher delay values (faster animations) result in smaller GIF files since fewer duplicate frames are perceived.
Troubleshooting
Animation not created
Error message:"Error writing frames" or "Error saving gif"
Solutions:
- Ensure you have write permissions in the current directory
- Check that graphviz is properly installed
- Verify that the
imageiopackage is installed - Try with a smaller input to rule out memory issues
Frames directory not deleted
If theframes/ directory persists after generation:
GIF plays too fast or too slow
Adjust thedelay parameter:
The
delay parameter controls frames per second (fps). Higher values = faster playback.