3b1b / manim

Animation engine for explanatory math videos
MIT License
62.98k stars 5.83k forks source link

Removing ticks in a graph scene #540

Open shar-anya opened 5 years ago

shar-anya commented 5 years ago

What is the best way to remove all the ticks on the axes?

I removed ticks by doing this:

class Function(GraphScene):
    CONFIG = {
    "x_min": -5,
    "x_max": 5,
    "y_min": -3,
    "y_max": 3,
    }
    def construct(self):
        self.x_leftmost_tick = self.x_max+1
        self.y_bottom_tick = self.y_max+1 

        self.setup_axes(animate = True)
        self.wait(3)

But doing this causes the x-axis label to appear at the origin, like so: image

To keep the x-label at it's usual position by default, in line 89 of manimlib/scene/graph_scene.py, I replaced x_axis.get_tick_marks(), UP + RIGHT, by x_axis.get_corner(UP + RIGHT), UP + RIGHT,

The y-axis label has a configuration similiar to the above line. Shouldn't that be the configuration for the x-axis label's position by default?

Kolloom commented 5 years ago

spent some time digging the source code.

The ticks are constructed with NumberLine class, the CONFIG in the NumberLine has a boolean value include_ticks, which defaults to True. You can monkey patch the CONFIG to turn the value to False.

when the boolean is set to false, this line, which sets up the tick_marks variable, does not get executed.

    if self.include_ticks:
        self.add_tick_marks()

However, the other method get_tick_marks expects tick_marks to be present. This method is called when GraphScene calls setup_axes

    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

which will also be executed if you want to have the x axis label. The y axis label uses get_corner which is what you did.

The code is not setup to handle no tick cases :(

Here is the code for monkey patching the CONFIG

CUSTOM_CONFIG = {
    "color": LIGHT_GREY,
    "x_min": -FRAME_X_RADIUS,
    "x_max": FRAME_X_RADIUS,
    "unit_size": 1,
    "include_ticks": False,  # this boolean controls tick generation
    "tick_size": 0.1,
    "tick_frequency": 1,
    # Defaults to value near x_min s.t. 0 is a tick
    # TODO, rename this
    "leftmost_tick": None,
    # Change name
    "numbers_with_elongated_ticks": [0],
    "include_numbers": False,
    "numbers_to_show": None,
    "longer_tick_multiple": 2,
    "number_at_center": 0,
    "number_scale_val": 0.75,
    "label_direction": DOWN,
    "line_to_number_buff": MED_SMALL_BUFF,
    "include_tip": False,
    "tip_width": 0.25,
    "tip_height": 0.25,
    "decimal_number_config": {"num_decimal_places": 0},
    "exclude_zero_from_default_numbers": False,
}

NumberLine.CONFIG = CUSTOM_CONFIG
# NumberLine.get_tick_marks = None