Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a scene with multiple graphs at once

Many different disciplines, Physics or Economics, for instance, require displaying multiple graphs at once, usually one on top of each other in order to demonstrate how some shock or event impacts different variables. How could this be done in Manim?

like image 270
shannontesla Avatar asked Jan 25 '23 15:01

shannontesla


1 Answers

First of all, let's paste the solution's code.

class PlotTwoGraphsAtOnce(GraphScene):
CONFIG = {
    "y_max" : 40,
    "y_min" : 0,
    "x_max" : 7,
    "x_min" : 0,
    "y_tick_frequency" : 10,
    "x_tick_frequency" : 1,
    "x_axis_width": 6,
    "y_axis_height": 3,
    "axes_color" : GRAY, 
}
def construct(self):
    self.graph_origin = -0.5 * DOWN + 3 * LEFT
    self.setup_axes(animate=True)
    graph_up = self.get_graph(lambda x : x**2, 
                                color = GOLD_A,
                                x_min = 0, 
                                x_max = 3
                                )
    f1 = TexMobject(r"f(x) = {x}^2", color = GOLD_A)
    f1.scale(0.7)
    label_coord1 = self.input_to_graph_point(3,graph_up)
    f1.next_to(label_coord1,RIGHT+UP)
    self.graph_origin = 3.5 * DOWN + 3 * LEFT
    self.setup_axes(animate=True)
    graph_down = self.get_graph(lambda x : x**3, 
                                color = BLUE_D,
                                x_min = 0, 
                                x_max = 3
                                )
    graphs=VGroup(graph_up,graph_down)
    f2 = TexMobject(r"f(x) = {x}^3", color = BLUE_D)
    f2.scale(0.7)
    label_coord2 = self.input_to_graph_point(3,graph_up)
    f2.next_to(label_coord2,RIGHT+UP)
    self.play(
        ShowCreation(graphs),
        run_time = 2,
    )
    self.play(ShowCreation(f1), ShowCreation(f2))
    self.wait(3)

And here's a visual preview of the final result.

x**2 and x**3 displayed in two graphs, one of top of the other

For the implementation of this solution I took inspiration from this answer.

like image 177
shannontesla Avatar answered Feb 08 '23 19:02

shannontesla