number_line

class janim.items.coordinate.number_line.NumberLine(x_range: RangeSpecifier = (-8, 8, 1), *, unit_size: int = 1, width: float | None = None, length: float | None = None, center: bool = True, color: JAnimColor = '#BBBBBB', stroke_radius: float = 0.01, include_tip: bool = False, tip_config: dict = {}, include_ticks: bool = True, tick_size: float = 0.1, longer_tick_multiple: float = 1.5, numbers_with_elongated_ticks: Iterable[float] = [], include_numbers: bool = False, numbers_to_exclude: Iterable[float] = [], line_to_number_direction: ndarray = array([0., -1., 0.]), line_to_number_buff: float = 0.25, decimal_number_config: dict = {}, **kwargs)

数轴

参数:

  • x_range:

    使用 [最小值, 最大值, 步长] 设定数轴的表示范围以及刻度步长

    或使用 [最小值, 最大值],步长默认为 1

  • unit_size: 数轴单位长度

  • length: 数轴总长,当该值设置时,会忽略 unit_size

  • center: 创建后是否使整体居中

箭头参数:

  • include_tip: 是否显示箭头,默认为 False 即不显示

  • tip_config: 提供给箭头的额外参数,另见 add_tip()

刻度参数:

  • include_ticks: 是否显示刻度,默认为 True 即显示

  • tick_size: 刻度大小

  • longer_tick_multiple: 长刻度在 tick_size 基础上的倍数

  • numbers_with_elongated_ticks: 提供一个列表,指定哪些数字是长刻度

数字参数:

  • include_numbers: 是否显示数字

  • numbers_to_exclude: 需要排除的数字

  • line_to_number_direction: 数字放在刻度点的哪个方向

  • line_to_number_buff: 数字与刻度点的间距

tip_config_d = {'back_width': 0.25, 'body_length': 0.25}

箭头的默认属性

decimal_number_config_d = {'font_size': 16, 'num_decimal_places': 0}

数字的默认属性

property tip: ArrowTip | None
property ticks: Group[Line] | None
property numbers: Group[Text] | None
get_unit_size() float
get_tick_range() ndarray
static compute_tick_range(x_min: float, x_max: float, x_step: float) ndarray
add_ticks(excluding: Iterable[float] | None = None) Group[Line]
get_tick(x: float, size: float | None = None) Line
add_numbers(x_values: Iterable[float] | None = None, excluding: Iterable[float] | None = None, font_size: int = 24, **kwargs) Group[Text]
get_number_item(x: float, direction: ndarray | None = None, buff: float | None = None, **number_config) Text
get_axis_label(label: str | Points, *, alpha: float = 1, direction: Vect = array([1., 1., 0.]), buff=0.25, ensure_on_screen: bool = False, **kwargs) TypstMath | Points

得到坐标轴标签文字

如果 label 是字符串,则会将其作为 Typst 公式解析成为 TypstMath 物件

物件会被放置到合适的地方,默认情况下是坐标轴尖端的旁边,具体来说由 alpha directionbuff 控制:

  • alpha 控制物件被放到坐标轴哪个点的旁边,该数值表示坐标轴上的百分比位置,例如默认的 1 即为坐标轴末尾,0 则为坐标轴起点

  • direction 控制物件被放到前面所述的点的哪个方向,默认是 UR 表示右上方

  • buff 控制物件与前面所述的点的间距

如果坐标轴比较长,坐标轴标签有可能会超出屏幕, 此时如果设置 ensure_on_screen=True,坐标轴标签会自动调整位置移动到默认屏幕区域内

number_to_point(number: float | Iterable[float] | ndarray) ndarray

传入数值得到在坐标轴上对应的位置

传入的可以是:

  • 单个数,得到单个坐标,表示这个数在坐标轴上的位置; 例如 n2p(2) 得到 2 在坐标轴上的位置

  • 多个数,得到一组坐标,分别表示这些数在坐标轴上的位置; 例如 n2p([0, 2, 4]) 分别得到 0、2、4 在坐标轴上的位置

point_to_number(point: ndarray) float

传入坐标将其映射到坐标轴上,返回在坐标轴上的数值

n2p(number: float) ndarray

number_to_point 的缩写

p2n(point: ndarray) float

point_to_number 的缩写

class janim.items.coordinate.number_line.UnitInterval(x_range: RangeSpecifier = (0, 1, 0.1), *, unit_size: int = 10, numbers_with_elongated_ticks: Iterable[float] = [0, 1], decimal_number_config: dict = {'num_decimal_places': 1}, **kwargs)

单位长度数轴(只有 0~1 的区段,其中细分 10 段)