0
  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

twoy
  • 53
  • 7
  • Note I got the floats working with the following: ``` def construct(self): self.setup_axes(animate=True) self.x_axis.add_numbers( *np.array([0.1,0.3,1]), number_config={ "num_decimal_places": 1, } ) ``` but this broke the animation – twoy Nov 22 '19 at 11:18

2 Answers2

0

I solved this with the solution here: https://youtu.be/YVxhhi14Ha0?list=PL2B6OzTsMUrwo4hA3BBfS7ZR34K361Z8F&t=329

  1. You have to manually add a custom parameter to the GraphScene class. You can name this anything, I called it x_label_decimals.

  2. You then pass that in to the NumberLine() call in the x_axis parameter creation inside the setup_axes function definition:

  3. You can then set the x_label_decimals parameter in your instantiation of your GraphScene.

# example_graph.py
class MyLinePlot(GraphScene):
     CONFIG = {
        ...
        "x_label_decimals": 2,
     }

# graph_scene.py
class GraphScene(Scene):
    CONFIG = {
        "x_min": -1
        ...
        # Added the below line inside the CONFIG object
        "x_label_decimals": 0,
    }

    ...

    def setup_axes(self, animate=False):
        ...
        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 the below line where we pass new x_label_decimals param
            decimal_number_config={
                "num_decimal_places": self.x_label_decimals,
            },

        )

twoy
  • 53
  • 7
0

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.

SirMallard
  • 16
  • 2