コード例 #1
0
    def construct(self):
        number_line = NumberLine(x_min=-2, x_max=2)
        text_1 = TextMobject("Theorem of") \
            .next_to(number_line, DOWN)
        text_2 = TextMobject("Beethoven") \
            .next_to(number_line, DOWN)
        dashed_line = DashedLine(
            number_line.get_left(),
            number_line.get_right(),
            color=YELLOW,
        ).set_stroke(width=11)

        self.add(number_line, text_1)

        self.play(
            LaggedStart(*[
                ShowCreationThenDestruction(dashed_segment)
                for dashed_segment in dashed_line
            ],
                        run_time=5),
            AnimationGroup(Animation(Mobject(), run_time=2.1),
                           ReplacementTransform(text_1, text_2),
                           lag_ratio=1))

        self.wait()
コード例 #2
0
ファイル: coordinate_systems.py プロジェクト: yk616/manim
 def create_axis(self, range_terms: Sequence[float], axis_config: dict[str],
                 length: float) -> NumberLine:
     new_config = merge_dicts_recursively(self.axis_config, axis_config)
     new_config["width"] = length
     axis = NumberLine(range_terms, **new_config)
     axis.shift(-axis.n2p(0))
     return axis
コード例 #3
0
    def construct(self):
        number_line = NumberLine(x_min=-1, x_max=1)
        triangle = RegularPolygon(3, start_angle=-PI / 2) \
            .scale(0.2) \
            .next_to(number_line.get_left(), UP, buff=SMALL_BUFF)
        decimal = DecimalNumber(
            0,
            num_decimal_places=3,
            include_sign=True,
            unit="\\rm cm",  # Change this with None
        )

        decimal.add_updater(lambda d: d.next_to(triangle, UP * 0.1))
        decimal.add_updater(lambda d: d.set_value(triangle.get_center()[0]))
        #       You can get the value of decimal with: .get_value()

        self.add(number_line, triangle, decimal)

        self.play(
            triangle.shift,
            RIGHT * 2,
            rate_func=there_and_back,  # Change this with: linear,smooth
            run_time=5)

        self.wait()
コード例 #4
0
    def construct(self):
        number_line = NumberLine(x_min=-2, x_max=2)
        triangle = RegularPolygon(3, start_angle=-PI / 2) \
            .scale(0.2) \
            .next_to(number_line.get_left(), UP, buff=SMALL_BUFF)
        numbers = VGroup(
            *[TextMobject("%s" % i) \
                  .next_to(number_line.get_tick(i - 2), DOWN) for i in range(1, 5)]
        )

        self.add(number_line)
        self.play(ShowCreation(triangle))
        self.wait(0.3)

        self.play(
            ApplyMethod(triangle.shift,
                        RIGHT * 4,
                        rate_func=linear,
                        run_time=4),
            *[
                AnimationGroup(Animation(Mobject(), run_time=i + 1),
                               Write(numbers[i]),
                               lag_ratio=1) for i in range(4)
            ],
        )

        self.wait()
コード例 #5
0
 def get_axis(self, min_val, max_val, axis_config):
     new_config = merge_config([
         axis_config,
         {"x_min": min_val, "x_max": max_val},
         self.number_line_config,
     ])
     return NumberLine(**new_config)
コード例 #6
0
 def create_axis(self, min_val, max_val, axis_config):
     new_config = merge_dicts_recursively(
         self.axis_config,
         {"x_min": min_val, "x_max": max_val},
         axis_config,
     )
     return NumberLine(**new_config)
コード例 #7
0
ファイル: coordinate_systems.py プロジェクト: yk616/manim
    def get_lines_parallel_to_axis(self, axis1: NumberLine,
                                   axis2: NumberLine) -> tuple[VGroup, VGroup]:
        freq = axis2.x_step
        ratio = self.faded_line_ratio
        line = Line(axis1.get_start(), axis1.get_end())
        dense_freq = (1 + ratio)
        step = (1 / dense_freq) * freq

        lines1 = VGroup()
        lines2 = VGroup()
        inputs = np.arange(axis2.x_min, axis2.x_max + step, step)
        for i, x in enumerate(inputs):
            new_line = line.copy()
            new_line.shift(axis2.n2p(x) - axis2.n2p(0))
            if i % (1 + ratio) == 0:
                lines1.add(new_line)
            else:
                lines2.add(new_line)
        return lines1, lines2
コード例 #8
0
    def construct(self):
        number_line = NumberLine(x_min=-1, x_max=1)
        triangle = RegularPolygon(3, start_angle=-PI / 2) \
            .scale(0.2) \
            .next_to(number_line.get_left(), UP, buff=SMALL_BUFF)

        def update_t(triangle, dt):
            triangle.shift(RIGHT * dt)

        self.add(number_line, triangle)

        self.wait(0.3)
        triangle.shift(LEFT)
        triangle.add_updater(update_t)

        # The animation begins
        self.wait(2)

        triangle.clear_updaters()
        self.wait()
コード例 #9
0
ファイル: coordinate_systems.py プロジェクト: pyang2005/manim
 def __init__(self, **kwargs):
     VGroup.__init__(self, **kwargs)
     x_axis_config = merge_config([
         self.x_axis_config,
         {
             "x_min": self.x_min,
             "x_max": self.x_min
         },
         self.number_line_config,
     ])
     y_axis_config = merge_config([
         self.y_axis_config,
         {
             "x_min": self.y_min,
             "x_max": self.y_min
         },
         self.number_line_config,
     ])
     self.x_axis = NumberLine(**x_axis_config)
     self.y_axis = NumberLine(**y_axis_config)
     self.y_axis.rotate(90 * DEGREES, about_point=ORIGIN)
     self.add(self.x_axis, self.y_axis)
コード例 #10
0
    def construct(self):
        number_line = NumberLine(x_min=-2, x_max=2)
        text = TextMobject("Text") \
            .next_to(number_line, DOWN)
        dashed_line = DashedLine(
            number_line.get_left(),
            number_line.get_right(),
            color=YELLOW,
        ).set_stroke(width=11)

        self.add(number_line)
        self.wait(0.3)

        self.play(
            LaggedStart(*[
                ShowCreationThenDestruction(dashed_segment)
                for dashed_segment in dashed_line
            ],
                        run_time=5),
            AnimationGroup(Animation(Mobject(), run_time=2.1),
                           Write(text),
                           lag_ratio=1))
        self.wait()
コード例 #11
0
 def create_axis(self, *args):
     if args[2] is not None and not isinstance(args[2], (int, float)):
         min_val, max_val, axis_config = args
         new_config = merge_dicts_recursively(
             self.number_line_config,
             {
                 "x_min": min_val,
                 "x_max": max_val
             },
             axis_config,
         )
         return NumberLine(**new_config)
     else:
         range_terms, axis_config, length = args
         new_config = merge_dicts_recursively(self.axis_config, axis_config)
         new_config["width"] = length
         axis = NumberLine(range_terms, **new_config)
         axis.shift(-axis.n2p(0))
         return axis
コード例 #12
0
    def construct(self):
        number_line = NumberLine(x_min=-2, x_max=2)
        triangle = RegularPolygon(3, start_angle=-PI / 2) \
            .scale(0.2) \
            .next_to(number_line.get_left(), UP, buff=SMALL_BUFF)
        text_1 = TextMobject("1") \
            .next_to(number_line.get_tick(-1), DOWN)
        text_2 = TextMobject("2") \
            .next_to(number_line.get_tick(0), DOWN)
        text_3 = TextMobject("3") \
            .next_to(number_line.get_tick(1), DOWN)
        text_4 = TextMobject("4") \
            .next_to(number_line.get_tick(2), DOWN)

        self.add(number_line)
        self.play(ShowCreation(triangle))
        self.wait(0.3)

        self.play(
            ApplyMethod(triangle.shift,
                        RIGHT * 4,
                        rate_func=linear,
                        run_time=4),
            AnimationGroup(Animation(Mobject(), run_time=1),
                           Write(text_1),
                           lag_ratio=1),
            AnimationGroup(Animation(Mobject(), run_time=2),
                           Write(text_2),
                           lag_ratio=1),
            AnimationGroup(Animation(Mobject(), run_time=3),
                           Write(text_3),
                           lag_ratio=1),
            AnimationGroup(Animation(Mobject(), run_time=4),
                           Write(text_4),
                           lag_ratio=1))

        self.wait()
コード例 #13
0
    def setup_axes(self, reback=False, animate=False):
        """
        This method sets up the axes of the graph.
        Parameters
        ----------
        animate (bool=False)
            Whether or not to animate the setting up of the Axes.
        """
        # reback和animate都为True时,只有reback生效
        # TODO, once eoc is done, refactor this to be less redundant.
        x_num_range = float(self.x_max - self.x_min)
        self.space_unit_to_x = self.x_axis_width / x_num_range
        if self.x_labeled_nums is None:
            self.x_labeled_nums = []
        if self.x_leftmost_tick is None:
            self.x_leftmost_tick = self.x_min
        x_axis = NumberLine(
            x_min=self.x_min,
            x_max=self.x_max,
            unit_size=self.space_unit_to_x,
            tick_frequency=self.x_tick_frequency,
            leftmost_tick=self.x_leftmost_tick,
            numbers_with_elongated_ticks=self.x_labeled_nums,
            color=self.axes_color,
            stroke_opacity=self.xyStrokeOpacity,
            decimal_number_config={"num_decimal_places": self.x_num_decimal_places}
        )
        x_axis.shift(self.graph_origin - x_axis.number_to_point(0))
        if len(self.x_labeled_nums) > 0:
            if self.exclude_zero_label:
                self.x_labeled_nums = [x for x in self.x_labeled_nums]
                # self.x_labeled_nums = [x for x in self.x_labeled_nums if x != 0]
            x_axis.add_numbers(*self.x_labeled_nums)
        if self.x_axis_label:
            x_label = TextMobject(self.x_axis_label)
            x_label.next_to(
                x_axis.get_tick_marks(), UP + RIGHT,
                buff=SMALL_BUFF
            )
            x_label.shift_onto_screen()
            x_axis.add(x_label)
            self.x_axis_label_mob = x_label

        y_num_range = float(self.y_max - self.y_min)
        self.space_unit_to_y = self.y_axis_height / y_num_range

        if self.y_labeled_nums is None:
            self.y_labeled_nums = []
        if self.y_bottom_tick is None:
            self.y_bottom_tick = self.y_min
        y_axis = NumberLine(
            x_min=self.y_min,
            x_max=self.y_max,
            unit_size=self.space_unit_to_y,
            tick_frequency=self.y_tick_frequency,
            leftmost_tick=self.y_bottom_tick,
            numbers_with_elongated_ticks=self.y_labeled_nums,
            color=self.axes_color,
            line_to_number_vect=LEFT,
            label_direction=LEFT,
            stroke_opacity=self.xyStrokeOpacity,
            decimal_number_config={"num_decimal_places": self.y_num_decimal_places}
        )
        y_axis.shift(self.graph_origin - y_axis.number_to_point(0))
        y_axis.rotate(np.pi / 2, about_point=y_axis.number_to_point(0))
        if len(self.y_labeled_nums) > 0:
            if self.exclude_zero_label:
                self.y_labeled_nums = [y for y in self.y_labeled_nums if y != 0]
            y_axis.add_numbers(*self.y_labeled_nums)
        if self.y_axis_label:
            y_label = TextMobject(self.y_axis_label)
            y_label.next_to(
                y_axis.get_corner(UP + RIGHT), UP + RIGHT,
                buff=SMALL_BUFF
            )
            y_label.shift_onto_screen()
            y_axis.add(y_label)
            self.y_axis_label_mob = y_label
        
        # 给对象绑定x_axis和y_axis属性
        # Ag 修改了reback和这里的顺序
        self.x_axis, self.y_axis = self.axes = VGroup(x_axis, y_axis)
        self.default_graph_colors = it.cycle(self.default_graph_colors)

        if self.add_coordinate_grid:
            self.lines_x_axis = self.get_vertical_lines_to_axis(
                num_lines=len(np.arange(self.x_min, self.x_max, self.x_tick_frequency))+1
                )
            self.lines_y_axis = self.get_horizontal_lines_to_axis(
                num_lines=len(np.arange(self.y_min, self.y_max, self.y_tick_frequency))+1
                )
            
        return self.reback_or_anim_axis(reback, animate)
コード例 #14
0
    def setup_axes(self, animate=False):
        """
        Creates two axes according to the parameters in CONFIG
        """
        # TODO, once eoc is done, refactor this to be less redundant.
        x_num_range = float(self.x_max - self.x_min)
        self.space_unit_to_x = self.x_axis_width / x_num_range
        if self.x_labeled_nums is None:
            self.x_labeled_nums = []
        if self.x_leftmost_tick is None:
            self.x_leftmost_tick = self.x_min
        x_axis = NumberLine(x_min=self.x_min,
                            x_max=self.x_max,
                            unit_size=self.space_unit_to_x,
                            tick_frequency=self.x_tick_frequency,
                            leftmost_tick=self.x_leftmost_tick,
                            numbers_with_elongated_ticks=self.x_labeled_nums,
                            color=self.axes_color)
        x_axis.shift(self.graph_origin - x_axis.number_to_point(0))
        if len(self.x_labeled_nums) > 0:
            if self.exclude_zero_label:
                self.x_labeled_nums = [
                    x for x in self.x_labeled_nums if x != 0
                ]
            x_axis.add_numbers(*self.x_labeled_nums)
        if self.x_axis_label:
            x_label = TextMobject(self.x_axis_label)
            x_label.next_to(x_axis.get_tick_marks(),
                            UP + RIGHT,
                            buff=SMALL_BUFF)
            x_label.shift_onto_screen()
            x_axis.add(x_label)
            self.x_axis_label_mob = x_label

        y_num_range = float(self.y_max - self.y_min)
        self.space_unit_to_y = self.y_axis_height / y_num_range

        if self.y_labeled_nums is None:
            self.y_labeled_nums = []
        if self.y_bottom_tick is None:
            self.y_bottom_tick = self.y_min
        y_axis = NumberLine(
            x_min=self.y_min,
            x_max=self.y_max,
            unit_size=self.space_unit_to_y,
            tick_frequency=self.y_tick_frequency,
            leftmost_tick=self.y_bottom_tick,
            numbers_with_elongated_ticks=self.y_labeled_nums,
            color=self.axes_color,
            line_to_number_vect=LEFT,
            label_direction=LEFT,
        )
        y_axis.shift(self.graph_origin - y_axis.number_to_point(0))
        y_axis.rotate(np.pi / 2, about_point=y_axis.number_to_point(0))
        if len(self.y_labeled_nums) > 0:
            if self.exclude_zero_label:
                self.y_labeled_nums = [
                    y for y in self.y_labeled_nums if y != 0
                ]
            y_axis.add_numbers(*self.y_labeled_nums)
        if self.y_axis_label:
            y_label = TextMobject(self.y_axis_label)
            y_label.next_to(y_axis.get_corner(UP + RIGHT),
                            UP + RIGHT,
                            buff=SMALL_BUFF)
            y_label.shift_onto_screen()
            y_axis.add(y_label)
            self.y_axis_label_mob = y_label

        if animate:
            self.play(Write(VGroup(x_axis, y_axis)))
        else:
            self.add(x_axis, y_axis)
        self.x_axis, self.y_axis = self.axes = VGroup(x_axis, y_axis)
        self.default_graph_colors = it.cycle(self.default_graph_colors)
コード例 #15
0
 def __init__(self, **kwargs):
     digest_config(self, kwargs)
     self.number_at_center = self.log_factor
     NumberLine.__init__(self, **kwargs)
コード例 #16
0
ファイル: coordinate_systems.py プロジェクト: pyang2005/manim
class Axes(VGroup):
    CONFIG = {
        "propagate_style_to_family": True,
        "three_d": False,
        "number_line_config": {
            "color": LIGHT_GREY,
            "include_tip": True,
        },
        "x_axis_config": {},
        "y_axis_config": {
            "label_direction": LEFT,
        },
        "x_min": -FRAME_X_RADIUS,
        "x_max": FRAME_X_RADIUS,
        "y_min": -FRAME_Y_RADIUS,
        "y_max": FRAME_Y_RADIUS,
        "default_num_graph_points": 100,
    }

    def __init__(self, **kwargs):
        VGroup.__init__(self, **kwargs)
        x_axis_config = merge_config([
            self.x_axis_config,
            {
                "x_min": self.x_min,
                "x_max": self.x_min
            },
            self.number_line_config,
        ])
        y_axis_config = merge_config([
            self.y_axis_config,
            {
                "x_min": self.y_min,
                "x_max": self.y_min
            },
            self.number_line_config,
        ])
        self.x_axis = NumberLine(**x_axis_config)
        self.y_axis = NumberLine(**y_axis_config)
        self.y_axis.rotate(90 * DEGREES, about_point=ORIGIN)
        self.add(self.x_axis, self.y_axis)

    def coords_to_point(self, *coords):
        origin = self.x_axis.number_to_point(0)
        result = np.array(origin)
        for axis, coord in zip(self, coords):
            result += (axis.number_to_point(coord) - origin)
        return result

    def point_to_coords(self, point):
        return tuple([
            axis.point_to_number(point) for axis in self
            if isinstance(axis, NumberLine)
        ])

    def get_graph(self,
                  function,
                  num_graph_points=None,
                  x_min=None,
                  x_max=None,
                  **kwargs):
        kwargs["fill_opacity"] = kwargs.get("fill_opacity", 0)
        kwargs["num_anchor_points"] = \
            num_graph_points or self.default_num_graph_points
        x_min = x_min or self.x_min
        x_max = x_max or self.x_max
        graph = ParametricFunction(
            lambda t: self.coords_to_point(t, function(t)),
            t_min=x_min,
            t_max=x_max,
            **kwargs)
        graph.underlying_function = function
        return graph

    def input_to_graph_point(self, x, graph):
        if hasattr(graph, "underlying_function"):
            return self.coords_to_point(x, graph.underlying_function(x))
        else:
            # binary search
            lh, rh = 0, 1
            while abs(lh - rh) > 0.001:
                mh = np.mean([lh, rh])
                hands = [lh, mh, rh]
                points = list(map(graph.point_from_proportion, hands))
                lx, mx, rx = list(map(self.x_axis.point_to_number, points))
                if lx <= x and rx >= x:
                    if mx > x:
                        rh = mh
                    else:
                        lh = mh
                elif lx <= x and rx <= x:
                    return points[2]
                elif lx >= x and rx >= x:
                    return points[0]
                elif lx > x and rx < x:
                    lh, rh = rh, lh
            return points[1]
        return self.coords_to_point(x, graph.underlying_function(x))
コード例 #17
0
 def setup_axes(self, animate=False):
     # X axis ---------------------
     x_num_range = float(self.x_max - self.x_min)
     self.space_unit_to_x = self.x_axis_width / x_num_range
     if self.x_labeled_nums is None:
         self.x_labeled_nums = []
     if self.x_elongated_nums is None:
         self.x_elongated_nums = []
     if self.x_leftmost_tick is None:
         self.x_leftmost_tick = self.x_min
     x_axis = NumberLine(x_min=self.x_min,
                         x_max=self.x_max,
                         unit_size=self.space_unit_to_x,
                         tick_frequency=self.x_tick_frequency,
                         leftmost_tick=self.x_leftmost_tick,
                         numbers_with_elongated_ticks=self.x_elongated_nums,
                         color=self.axes_color)
     if len(self.x_labeled_nums) > 0:
         if self.exclude_zero_label:
             self.x_labeled_nums = [
                 x for x in self.x_labeled_nums if x != 0
             ]
         x_axis.add_numbers(*self.x_labeled_nums)
     if self.x_axis_label:
         x_label = TextMobject(self.x_axis_label)
         x_label.next_to(
             x_axis.get_corner(self.x_axis_label_position),
             self.x_axis_label_position,
             buff=self.x_axis_label_buff,
         )
         x_axis.add(x_label)
         self.x_axis_label_mob = x_label
     # Y axis ---------------------
     y_num_range = float(self.y_max - self.y_min)
     self.space_unit_to_y = self.y_axis_height / y_num_range
     if self.y_labeled_nums is None:
         self.y_labeled_nums = []
     if self.y_elongated_nums is None:
         self.y_elongated_nums = []
     if self.y_bottom_tick is None:
         self.y_bottom_tick = self.y_min
     y_axis = NumberLine(
         x_min=self.y_min,
         x_max=self.y_max,
         unit_size=self.space_unit_to_y,
         tick_frequency=self.y_tick_frequency,
         leftmost_tick=self.y_bottom_tick,
         numbers_with_elongated_ticks=self.y_elongated_nums,
         color=self.axes_color,
         label_direction=LEFT,
     )
     y_axis.shift(-1 * y_axis.number_to_point(0))
     y_axis.rotate(np.pi / 2, about_point=y_axis.number_to_point(0))
     if len(self.y_labeled_nums) > 0:
         if self.exclude_zero_label:
             self.y_labeled_nums = [
                 y for y in self.y_labeled_nums if y != 0
             ]
         y_axis.add_numbers(*self.y_labeled_nums)
     if self.y_axis_label:
         y_label = TextMobject(self.y_axis_label)
         y_label.next_to(
             y_axis.get_corner(self.y_axis_label_position),
             self.y_axis_label_position,
             buff=self.y_axis_label_buff,
         )
         y_axis.add(y_label)
         self.y_axis_label_mob = y_label
     # Add axes
     self.add(x_axis, y_axis)
     self.x_axis, self.y_axis = self.axes = VGroup(x_axis, y_axis)
     self.default_graph_colors = it.cycle(self.default_graph_colors)
コード例 #18
0
ファイル: graph_scene.py プロジェクト: coallaoh/manim
    def setup_axes(self, animate=False):
        # TODO, once eoc is done, refactor this to be less redundant.
        x_num_range = float(self.x_max - self.x_min)
        self.space_unit_to_x = self.x_axis_width / x_num_range
        if self.x_labeled_nums is None:
            self.x_labeled_nums = []
        if self.x_leftmost_tick is None:
            self.x_leftmost_tick = self.x_min
        x_axis = NumberLine(
            x_min=self.x_min,
            x_max=self.x_max,
            unit_size=self.space_unit_to_x,
            tick_frequency=self.x_tick_frequency,
            leftmost_tick=self.x_leftmost_tick,
            numbers_with_elongated_ticks=self.x_labeled_nums,
            color=self.axes_color
        )
        x_axis.shift(self.graph_origin - x_axis.number_to_point(0))
        if len(self.x_labeled_nums) > 0:
            if self.exclude_zero_label:
                self.x_labeled_nums = [x for x in self.x_labeled_nums if x != 0]
            x_axis.add_numbers(*self.x_labeled_nums)
        if self.x_axis_label:
            x_label = TextMobject(self.x_axis_label)
            x_label.next_to(
                x_axis.get_tick_marks(), UP + RIGHT,
                buff=SMALL_BUFF
            )
            x_label.shift_onto_screen()
            x_axis.add(x_label)
            self.x_axis_label_mob = x_label

        y_num_range = float(self.y_max - self.y_min)
        self.space_unit_to_y = self.y_axis_height / y_num_range

        if self.y_labeled_nums is None:
            self.y_labeled_nums = []
        if self.y_bottom_tick is None:
            self.y_bottom_tick = self.y_min
        y_axis = NumberLine(
            x_min=self.y_min,
            x_max=self.y_max,
            unit_size=self.space_unit_to_y,
            tick_frequency=self.y_tick_frequency,
            leftmost_tick=self.y_bottom_tick,
            numbers_with_elongated_ticks=self.y_labeled_nums,
            color=self.axes_color,
            line_to_number_vect=LEFT,
            label_direction=LEFT,
        )
        y_axis.shift(self.graph_origin - y_axis.number_to_point(0))
        y_axis.rotate(np.pi / 2, about_point=y_axis.number_to_point(0))
        if len(self.y_labeled_nums) > 0:
            if self.exclude_zero_label:
                self.y_labeled_nums = [y for y in self.y_labeled_nums if y != 0]
            y_axis.add_numbers(*self.y_labeled_nums)
        if self.y_axis_label:
            y_label = TextMobject(self.y_axis_label)
            y_label.next_to(
                y_axis.get_corner(UP + RIGHT), UP + RIGHT,
                buff=SMALL_BUFF
            )
            y_label.shift_onto_screen()
            y_axis.add(y_label)
            self.y_axis_label_mob = y_label

        if animate:
            self.play(Write(VGroup(x_axis, y_axis)))
        else:
            self.add(x_axis, y_axis)
        self.x_axis, self.y_axis = self.axes = VGroup(x_axis, y_axis)
        self.default_graph_colors = it.cycle(self.default_graph_colors)
コード例 #19
0
 def create_axis(self, range_terms, axis_config, length):
     new_config = merge_dicts_recursively(self.axis_config, axis_config)
     new_config["width"] = length
     axis = NumberLine(range_terms, **new_config)
     axis.shift(-axis.n2p(0))
     return axis
コード例 #20
0
    def construct(self):
        self._main_title()

        text_one = TextMobject("Given a list of items sold")
        text_two: TextMobject = TextMobject(
            "Randomly choose items matching this distribution")
        text_two.next_to(text_one, DOWN)
        number_line = NumberLine(
            numbers_with_elongated_ticks=[0, 1],
            include_numbers=True,
            x_min=0,
            x_max=1,
            unit_size=10,
            tick_frequency=0.1,
            # decimal_number_config={"num_decimal_places": 1},
            numbers_to_show=[0, 1])
        number_line.next_to(text_two, UP)

        self.play(ShowCreation(text_one))
        self.wait()
        self.play(ShowCreation(text_two))
        self.wait(4)

        apples_text = TextMobject("Apples:")
        apples_text.set_color(self._apple_colour)

        apples_text.to_edge(UP)
        apples_text.align_to(text_two, LEFT)

        apple_count_text = TextMobject(f"{self._apple_count}")
        apple_count_text.set_color(self._apple_colour)
        apple_count_text.next_to(apples_text, RIGHT)

        banana_text = TextMobject("Bananas:")
        banana_text.set_color(self._banana_colour)

        banana_text.next_to(apples_text, DOWN)
        banana_text.align_to(apples_text, LEFT)

        banana_count_text = TextMobject(f"{self._banana_count}")
        banana_count_text.set_color(self._banana_colour)
        banana_count_text.next_to(banana_text, RIGHT)

        self.play(Transform(text_one, apples_text))
        self.play(ShowCreation(apple_count_text))
        self.play(ShowCreation(banana_text), ShowCreation(banana_count_text))

        banana_bar = Rectangle(
            height=0.4,
            width=number_line.point_to_number(self._banana_fraction * 10) *
            (number_line.number_to_point(1)[0]),
            color=self._banana_colour,
            fill_color=self._banana_colour,
            fill_opacity=0.75)
        banana_bar.next_to(banana_count_text, RIGHT + RIGHT)

        apple_bar = Rectangle(
            height=0.4,
            width=number_line.point_to_number(self._apple_fraction * 10) *
            (number_line.number_to_point(1)[0]),
            color=self._apple_colour,
            fill_color=self._apple_colour,
            fill_opacity=0.75)
        apple_bar.next_to(banana_bar, UP)
        apple_bar.align_to(banana_bar, LEFT)

        self.play(FadeIn(apple_bar), FadeIn(banana_bar))

        self.wait(1.5)

        apple_fraction_text = TextMobject("$\\frac{" + str(self._apple_count) +
                                          "}{" + str(self._apple_count +
                                                     self._banana_count) +
                                          "} = " + str(self._apple_fraction) +
                                          "$")
        apple_fraction_text.next_to(apple_bar, RIGHT)

        banana_fraction_text = TextMobject("$\\frac{" +
                                           str(self._banana_count) + "}{" +
                                           str(self._apple_count +
                                               self._banana_count) + "} = " +
                                           str(self._banana_fraction) + "$")
        banana_fraction_text.next_to(banana_bar, RIGHT)

        self.play(ShowCreation(apple_fraction_text))
        self.play(ShowCreation(banana_fraction_text))

        self.wait(2)

        number_line_map_text = TextMobject(
            "Map these counts to values between 0 and 1")
        number_line_map_text.next_to(text_two, UP)
        self.play(ShowCreation(number_line_map_text))

        self.wait(3)
        self.play(Transform(number_line_map_text, number_line))

        apple_num_ln_bar = Rectangle(
            height=0.4,
            # width=1 - self._apple_fraction * (number_line.number_to_point(1)[0]),
            width=number_line.point_to_number(self._apple_fraction * 10) *
            (number_line.number_to_point(1)[0]),
            color=self._apple_colour,
            fill_color=self._apple_colour,
            fill_opacity=0.25)
        apple_num_ln_bar.move_to(apple_bar, LEFT)
        self.add(apple_num_ln_bar)
        self.wait(2)
        self.play(
            ApplyMethod(apple_num_ln_bar.move_to,
                        number_line.number_to_point(0), LEFT))

        banana_num_ln_bar = Rectangle(
            height=0.4,
            width=number_line.point_to_number(self._banana_fraction * 10) *
            (number_line.number_to_point(1)[0]),
            color=self._banana_colour,
            fill_color=self._banana_colour,
            fill_opacity=0.25)
        banana_num_ln_bar.move_to(banana_bar, LEFT)
        self.add(banana_num_ln_bar)
        self.wait(2)
        self.play(
            ApplyMethod(banana_num_ln_bar.move_to,
                        number_line.number_to_point(1), RIGHT))

        text_scale: float = 0.75
        get_rnd_full = TextMobject(
            "Get a random number $n$ between 0 and 1 (uniform distribution)")

        get_apple_text = TextMobject(
            f"Apple\\quad if $n <= {self._apple_fraction}$",
            tex_to_color_map={"Apple": self._apple_colour})
        get_banana_text = TextMobject(
            f"Banana\\quad if $n > {self._apple_fraction}$",
            tex_to_color_map={"Banana": self._banana_colour})

        get_rnd_full.scale(text_scale)
        get_rnd_full.next_to(text_two, DOWN)
        get_banana_text.next_to(get_apple_text, DOWN)
        step_group = VGroup(get_apple_text, get_banana_text)

        brace = Brace(step_group, LEFT)
        step_text_d = brace.get_text("$n \\sim U(0, 1)$")
        step_text_d.scale(text_scale)
        step_text_d.next_to(get_rnd_full, DOWN + DOWN)
        step_text_d.shift(LEFT)
        brace.next_to(step_text_d, RIGHT)

        step_group.scale(text_scale)
        step_group.next_to(step_text_d, RIGHT + RIGHT + RIGHT)

        self.wait(2)
        self.play(ShowCreation(get_rnd_full))
        self.wait(2)
        self.play(ShowCreation(step_text_d))
        self.wait(2)

        self.play(GrowFromCenter(brace))
        self.wait()
        self.play(ShowCreation(get_apple_text))
        self.wait(2)
        self.play(ShowCreation(get_banana_text))

        # random_nos_to_draw = 10
        # main_arrow = Arrow(ORIGIN, DOWN * 1.3)
        # helper_arrow = Arrow(ORIGIN, LEFT * 1.3)
        #
        # for i in range(random_nos_to_draw):
        #   num: float = np.random.random_sample(1)
        #   point = number_line.number_to_point(num)
        #   arrow_colour = self._apple_colour if num <= self._apple_fraction else self._banana_colour
        #   arrow_recipient = get_apple_text if num <= self._apple_fraction else get_banana_text
        #
        #   main_arrow.set_color(arrow_colour)
        #
        #   if i == 0:
        #     main_arrow.next_to(point, UP)
        #     helper_arrow.next_to(arrow_recipient, RIGHT)
        #     self.play(GrowArrow(main_arrow), GrowArrow(helper_arrow))
        #   else:
        #     self.play(ApplyMethod(helper_arrow.next_to, arrow_recipient, RIGHT),
        #               ApplyMethod(main_arrow.next_to, point, UP))
        #   self.wait()
        #
        # self.play(FadeOut(main_arrow), FadeOut(helper_arrow))
        self.wait()
コード例 #21
0
    def setup_axes(self, animate=False):
        """
        This method sets up the axes of the graph.

        Parameters
        ----------
        animate (bool=False)
            Whether or not to animate the setting up of the Axes.
        """
        # TODO, once eoc is done, refactor this to be less redundant.
        x_num_range = float(self.x_max - self.x_min)
        self.space_unit_to_x = self.x_axis_width / x_num_range
        if self.x_labeled_nums is None:
            self.x_labeled_nums = []
        if self.x_leftmost_tick is None:
            self.x_leftmost_tick = self.x_min
        x_axis = NumberLine(
            x_min=self.x_min,
            x_max=self.x_max,
            unit_size=self.space_unit_to_x,
            tick_frequency=self.x_tick_frequency,
            leftmost_tick=self.x_leftmost_tick,
            numbers_with_elongated_ticks=self.x_labeled_nums,
            color=self.axes_color,
            # Added this line
            decimal_number_config={"color": self.label_nums_color})
        x_axis.shift(self.graph_origin - x_axis.number_to_point(0))
        if len(self.x_labeled_nums) > 0:
            if self.exclude_zero_label:
                self.x_labeled_nums = [
                    x for x in self.x_labeled_nums if x != 0
                ]
            x_axis.add_numbers(*self.x_labeled_nums)
        if self.x_axis_label:
            x_label = TextMobject(self.x_axis_label)

            # Added this line
            x_label.set_color(self.label_color)
            x_label.next_to(x_axis.get_tick_marks(),
                            UP + RIGHT,
                            buff=SMALL_BUFF)
            x_label.shift_onto_screen()
            x_axis.add(x_label)
            self.x_axis_label_mob = x_label
        y_num_range = float(self.y_max - self.y_min)
        self.space_unit_to_y = self.y_axis_height / y_num_range

        if self.y_labeled_nums is None:
            self.y_labeled_nums = []
        if self.y_bottom_tick is None:
            self.y_bottom_tick = self.y_min
        y_axis = NumberLine(
            x_min=self.y_min,
            x_max=self.y_max,
            unit_size=self.space_unit_to_y,
            tick_frequency=self.y_tick_frequency,
            leftmost_tick=self.y_bottom_tick,
            numbers_with_elongated_ticks=self.y_labeled_nums,
            color=self.axes_color,
            line_to_number_vect=LEFT,
            label_direction=LEFT,
            # yongze added this line
            decimal_number_config={"color": self.label_nums_color},
        )
        y_axis.shift(self.graph_origin - y_axis.number_to_point(0))
        y_axis.rotate(np.pi / 2, about_point=y_axis.number_to_point(0))
        if len(self.y_labeled_nums) > 0:
            if self.exclude_zero_label:
                self.y_labeled_nums = [
                    y for y in self.y_labeled_nums if y != 0
                ]
            y_axis.add_numbers(*self.y_labeled_nums)
        if self.y_axis_label:
            y_label = TextMobject(self.y_axis_label)
            # yongze Added this line
            y_label.set_color(self.label_color)
            y_label.next_to(y_axis.get_corner(UP + RIGHT),
                            UP + RIGHT,
                            buff=SMALL_BUFF)
            y_label.shift_onto_screen()
            y_axis.add(y_label)
            self.y_axis_label_mob = y_label

        if animate:
            self.play(Write(VGroup(x_axis, y_axis)))
        else:
            self.add(x_axis, y_axis)
        self.x_axis, self.y_axis = self.axes = VGroup(x_axis, y_axis)
        self.default_graph_colors = it.cycle(self.default_graph_colors)
コード例 #22
0
ファイル: coordinate_systems.py プロジェクト: yuzhang49/manim
 def get_axis(self, min_val, max_val, extra_config):
     config = dict(self.number_line_config)
     config.update(extra_config)
     return NumberLine(x_min=min_val, x_max=max_val, **config)