Search code examples
manim

How do I pass a float to x_axis


  1. I'm trying to create a GraphScene and plot a function between 0 and 1.
  2. I want labels at 0.1 and 0.3.
  3. I am attempting to achieve this by passing "x_labeled_nums": np.array([0.1, 0.3, 1]) and then "x_axis": {"decimal_number_config": {"num_decimal_places": 2}} in the CONFIG but I am still getting integers displayed on the x-axis where I am attempting to get it to render floats 0.1 and 0.3
class PlotFloats(GraphScene):
    CONFIG = {
        "x_min": 0,
        "x_max": 1,
        "x_axis_label": "X",
        "y_min": 0,
        "y_max": 2,
        "y_axis_label": "Y",
        "x_axis": {
            "decimal_number_config": {"num_decimal_places": 2}
        },
        "function_color": RED,
        "x_labeled_nums": np.array([0.1, 0.3, 1]),
    }

    def construct(self):
        self.setup_axes(animate=True)
        func_graph = self.get_graph(self.func_to_graph, self.function_color)

        self.play(ShowCreation(func_graph))

    def func_to_graph(self, x):
        return max(0.05, min(1, (0.95 / 0.2) * (x - 0.1)))

Link to output


Solution

  • I had the same issue as you and decided to look at the code behind manim to find why the number was rounding. It turns that you made a small error:

    Replace "x_axis"

    "x_axis": {
            "decimal_number_config": {"num_decimal_places": 2}
        },
    

    With "x_axis_config"

    "x_axis_config": {
            "decimal_number_config": {"num_decimal_places": 2}
        },
    

    Because the x_axis_config has the decimal_number_config setting.