0
  1. 我正在尝试创建一个 GraphScene 并绘制一个介于 0 和 1 之间的函数。
  2. 我想要 0.1 和 0.3 的标签。
  3. 我试图通过传递"x_labeled_nums": np.array([0.1, 0.3, 1])然后"x_axis": {"decimal_number_config": {"num_decimal_places": 2}}在中来实现这一点,CONFIG但我仍然在 x 轴上显示整数,我试图让它渲染浮点数 0.1 和 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)))

链接到输出

4

2 回答 2

0

我用这里的解决方案解决了这个问题:https ://youtu.be/YVxhhi14Ha0?list=PL2B6OzTsMUrwo4hA3BBfS7ZR34K361Z8F&t=329

  1. 您必须手动向GraphScene类添加自定义参数。你可以给它起任何名字,我叫它x_label_decimals

  2. 然后,您将其传递给函数定义内的参数创建中的NumberLine()调用:x_axissetup_axes

  3. x_label_decimals然后,您可以在 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,
            },

        )

于 2019-11-26T18:36:01.007 回答
0

我和你有同样的问题,并决定查看 manim 背后的代码以找出数字四舍五入的原因。原来你犯了一个小错误:

替换“x_axis”

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

使用“x_axis_config”

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

因为 x_axis_config 具有 decimal_number_config 设置。

于 2021-05-04T20:21:28.753 回答