*The post is part of a series on learning how to use manim. You can find the previous tutorial post in this series here and the overview of the entire series here.*

**Important Note: These posts are based on an earlier version of manim which uses Python 2.7. The latest version of manim is using Python 3. To follow along with these posts, use Python 2.7 and the May 9, 2018 commit of manim .**

# 8.0 More Graphing

In the previous post in this series we looked at how to graph functions in manim so go take a look at this if you haven’t already. I’ve been playing a bit more with graphing in manim and wanted to share some of what I’ve learned. You can copy and paste the code below or download the code from Github.

class ExampleApproximation(GraphScene): CONFIG = { "function" : lambda x : np.cos(x), "function_color" : BLUE, "taylor" : [lambda x: 1, lambda x: 1-x**2/2, lambda x: 1-x**2/math.factorial(2)+x**4/math.factorial(4), lambda x: 1-x**2/2+x**4/math.factorial(4)-x**6/math.factorial(6), lambda x: 1-x**2/math.factorial(2)+x**4/math.factorial(4)-x**6/math.factorial(6)+x**8/math.factorial(8), lambda x: 1-x**2/math.factorial(2)+x**4/math.factorial(4)-x**6/math.factorial(6)+x**8/math.factorial(8) - x**10/math.factorial(10)], "center_point" : 0, "approximation_color" : GREEN, "x_min" : -10, "x_max" : 10, "y_min" : -1, "y_max" : 1, "graph_origin" : ORIGIN , "x_labeled_nums" :range(-10,12,2), } def construct(self): self.setup_axes(animate=True) func_graph = self.get_graph( self.function, self.function_color, ) approx_graphs = [ self.get_graph( f, self.approximation_color ) for f in self.taylor ] term_num = [ TexMobject("n = " + str(n),aligned_edge=TOP) for n in range(0,8)] [t.to_edge(BOTTOM,buff=SMALL_BUFF) for t in term_num] term = TexMobject("") term.to_edge(BOTTOM,buff=SMALL_BUFF) approx_graph = VectorizedPoint( self.input_to_graph_point(self.center_point, func_graph) ) self.play( ShowCreation(func_graph), ) for n,graph in enumerate(approx_graphs): self.play( Transform(approx_graph, graph, run_time = 2), Transform(term,term_num[n]) ) self.wait()

I wanted to demonstrate how adding higher terms in a Taylor expansion results in better and better agreement with a function. This is similar to what

shinigamiphoenix posted here.

The functions to plot are defined as lambda functions in the `CONFIG{}`

dictionary. manim processes all elements in `CONFIG{}`

and turns the dictionary entries into class variables with the key as the variable name. Thus `"function"`

can be accessed within my class by calling `self.function`

and `"taylor"`

can be called with `self.taylor`

. If you aren’t familiar with lambda functions, check out this post at Python Conquers the Universe.

We create a list of graphs using `get_graph()`

and a list comprehension. You can find a nice tutorial on list comprehensions over at datacamp.com. It was only after reading this tutorial that I made the connection between list comprehensions and mathematical notation for definitions of sets (e.g. the set of positive real numbers is{ R and } or the set of even numbers which is { I and }), which made list comprehensions click for me. For each item in the list `self.taylor`

, a graph is created with color `self.approximation_color`

. We also created a list of TexMobjects to indicate which order of terms are included from the Taylor expansion using a list comprehension.

Since we are going to do successive transformations from a list, it helps to have a blank placeholder on the screen. `term`

and `approx_graph`

are `VectorizedPoint`

instances, which are mobjects that don’t display anything on screen. This way we can put the placeholders on the screen without anything appearing, and then transform those mobjects into either the graph or the TexMobjects.

The `enumerate()`

command is a useful tool that iterates over a list and also returns the index of the item returned. Thus `for n,graph in enumerate(approx_graphs)`

returns the index between 0 and 4 as `n`

, and the element within the list as `graph`

. This is used to display the corresponding item from `term_num`

with each graph.

Check out how to draw vector fields in the next post in this series.

Pingback: Graphing Functions – manim Series: Part 7 | Talking Physics

Pingback: Learning How To Animate Videos Using manim Series – A Journey | Talking Physics

Pingback: Vector Fields – manim Series: Part 9 | Talking Physics

Hey, first things first, I want to thank you for writing this series. It has been a great help and definitely helped me getting through the manim basics. While I was graphing some stuff, I ran into some functions that weren’t continuous at every point in its domain, so I was wondering if there’s a way to deal with discontinuous points when graphing using manim.