How to Draw a Graph in Manim?

By

Manim, short for Mathematical Animation Engine, is a powerful Python library used to create stunning animations, particularly for mathematical and educational content. Among its wide range of capabilities is the ability to draw and animate graphs, which makes it an excellent tool for visualizing mathematical functions. This guide walks you through the process of drawing a graph in Manim, explaining the essential components and steps involved.

Setting Up Manim

Setting Up Manim

Before you can draw a graph, you need to set up Manim on your system. Start by installing Manim through pip by running the command pip install manim. Ensure that your Python environment is ready and supports all necessary dependencies. Once installed, you can test your setup by running a basic Manim script.

To work with graphs in Manim, it is essential to import the relevant modules. Typically, this involves importing Scene and Axes or NumberPlane from manim.

Creating a Coordinate System

Graphs in Manim are drawn within a coordinate system, which can be created using the Axes or NumberPlane classes. The Axes class is ideal for creating standard x-y coordinate systems, while NumberPlane provides a grid-like structure.

To create a coordinate system, initialize an Axes object with parameters such as axis ranges, label sizes, and colors. For example:

from manim import *

class GraphScene(Scene):
def construct(self):
axes = Axes(
x_range=[-5, 5, 1],
y_range=[-5, 5, 1],
axis_config={“include_numbers”: True},
)
self.play(Create(axes))

This script sets up a coordinate system with x and y ranges from -5 to 5, including numbered labels.

Plotting a Graph

To plot a graph, you need a mathematical function and an axes object. Manim provides the plot method in the Axes class to generate graphs from Python functions. Define the function you want to plot and use plot to create the graph.

For instance, to plot :

graph = axes.plot(lambda x: x**2, color=BLUE)
self.play(Create(graph))

The lambda x: x**2 defines the function , and Create(graph) animates its appearance.

Adding Labels and Annotations

Manim allows you to add labels, titles, and other annotations to your graphs for better understanding. Use methods like add_labels or plot_label to include these elements.

For example:

graph_label = axes.get_graph_label(graph, label=”x^2″)
self.play(Write(graph_label))

This snippet adds a label “x^2” to the graph.

Customizing Graph Appearance

Manim provides flexibility in styling your graph. You can change the graph’s color, stroke width, and more by passing parameters to the plot method. For instance:

graph = axes.plot(lambda x: x**2, color=RED, stroke_width=4)

This creates a bold red graph for better visibility.

Rendering the Animation

After constructing your graph and animations, render the scene using the Manim CLI. Save your script in a .py file and run it using:

manim -pql your_script_name.py GraphScene

The -pql flag ensures that the output is of high quality and opens automatically for preview.

Drawing a graph in Manim involves setting up a coordinate system, plotting the desired function, and customizing the graph’s appearance. With its robust features and flexibility, Manim makes it easy to create visually appealing and informative graph animations. Experiment with different functions and styles to make the most of this powerful tool.