在 manim 中,如何将轴标签的文本设置为与轴单位不同?
In manim, how do you set the text of the axis labels different to the axis units?
我想在 x 轴上有年份,从 1970 年到 2019 年。但是当我在配置中将最小 x 设置为 1970 年时,x 轴没有出现(我假设是因为它默认从 0 开始如果我将 1970 年设置为第一年,那就太偏右了)。
我现在只能在 x 轴上显示单位文本。
我该如何解决这个问题?我的猜测是我需要告诉 manim 标签文本与轴的单位不同,但我不确定如何 - 或者即使这是正确的方法。
代码如下:
class TimeGraph(GraphFromData):
CONFIG = {
"x_min": 0,
"x_max": 49,
"x_tick_frequency": 5,
"unit_size": 2,
"y_min": 0,
"y_max": 600,
"y_tick_frequency": 100,
"unit_size": 0.5,
"x_axis_width": 12,
"y_axis_height": 6,
"axes_color": "#3A749C",
"x_label_color": "#3A749C",
"y_label_color": "#3A749C",
"label_nums_color": "#3A749C",
"y_label_direction": LEFT,
"x_label_direction": DOWN,
"camera_config": {
"background_color": WHITE,
},
"x_label_font_size": 0.5,
"y_label_font_size": 0.5,
}
def construct(self):
vector = np.array([-5, -3, 0])
self.graph_origin = vector
self.setup_axes()
# Get coords
coords = get_coords_from_excel(
"projects/carbon_debt/data_excel", sheet_name="unga_env"
)
dots = self.get_dots_from_coords(coords)
dots.set_color("#E89F99")
dots.set_stroke(color="#3A749C", width=1)
self.add(dots)
def setup_axes(self):
GraphScene.setup_axes(self)
init_label_x = 0
end_label_x = 45
step_x = 5
init_label_y = 100
end_label_y = 600
step_y = 100
self.x_axis.add_numbers(
*range(init_label_x, end_label_x + step_x, step_x),
)
self.y_axis.add_numbers(*range(init_label_y, end_label_y + step_y, step_y))
self.add(self.x_axis)
self.add(self.y_axis)
我明白了。
该解决方案由 TheoremOfBeethoven 课程提供,特别是关于二维绘图的课程,class 称为 Plot5(代码 here)
一种方法是用轴位置和轴标签创建一个 VGroup 元组,并在场景的 setup_axes 方法中,为每个标签创建一个 TexMobject 并将其定位在轴位置。添加到 setup_axes 方法的代码类似于以下内容。
除此 setup_axes 方法外,您显然可以通过遍历 x 轴单位值和所需标签列表来创建自己的 values_x。调用它 self.values_x 并从 setup_axes 方法中调用它。
values_x = [
(3.5, "3.5"), # (position 3.5, label "3.5")
(4.5, "\frac{9}{2}"), # (position 4.5, label "9/2")
]
self.x_axis_labels = VGroup() # Create a group named x_axis_labels
# pos. tex.
for x_val, x_tex in values_x:
tex = TexMobject(x_tex) # Convert string to tex
tex.set_color(self.label_nums_color)
tex.scale(self.x_label_font_size)
tex.next_to(self.coords_to_point(x_val, 0), DOWN) # Put tex on the position
self.x_axis_labels.add(tex) # Add tex in graph
我想在 x 轴上有年份,从 1970 年到 2019 年。但是当我在配置中将最小 x 设置为 1970 年时,x 轴没有出现(我假设是因为它默认从 0 开始如果我将 1970 年设置为第一年,那就太偏右了)。
我现在只能在 x 轴上显示单位文本。
我该如何解决这个问题?我的猜测是我需要告诉 manim 标签文本与轴的单位不同,但我不确定如何 - 或者即使这是正确的方法。
代码如下:
class TimeGraph(GraphFromData):
CONFIG = {
"x_min": 0,
"x_max": 49,
"x_tick_frequency": 5,
"unit_size": 2,
"y_min": 0,
"y_max": 600,
"y_tick_frequency": 100,
"unit_size": 0.5,
"x_axis_width": 12,
"y_axis_height": 6,
"axes_color": "#3A749C",
"x_label_color": "#3A749C",
"y_label_color": "#3A749C",
"label_nums_color": "#3A749C",
"y_label_direction": LEFT,
"x_label_direction": DOWN,
"camera_config": {
"background_color": WHITE,
},
"x_label_font_size": 0.5,
"y_label_font_size": 0.5,
}
def construct(self):
vector = np.array([-5, -3, 0])
self.graph_origin = vector
self.setup_axes()
# Get coords
coords = get_coords_from_excel(
"projects/carbon_debt/data_excel", sheet_name="unga_env"
)
dots = self.get_dots_from_coords(coords)
dots.set_color("#E89F99")
dots.set_stroke(color="#3A749C", width=1)
self.add(dots)
def setup_axes(self):
GraphScene.setup_axes(self)
init_label_x = 0
end_label_x = 45
step_x = 5
init_label_y = 100
end_label_y = 600
step_y = 100
self.x_axis.add_numbers(
*range(init_label_x, end_label_x + step_x, step_x),
)
self.y_axis.add_numbers(*range(init_label_y, end_label_y + step_y, step_y))
self.add(self.x_axis)
self.add(self.y_axis)
我明白了。 该解决方案由 TheoremOfBeethoven 课程提供,特别是关于二维绘图的课程,class 称为 Plot5(代码 here)
一种方法是用轴位置和轴标签创建一个 VGroup 元组,并在场景的 setup_axes 方法中,为每个标签创建一个 TexMobject 并将其定位在轴位置。添加到 setup_axes 方法的代码类似于以下内容。
除此 setup_axes 方法外,您显然可以通过遍历 x 轴单位值和所需标签列表来创建自己的 values_x。调用它 self.values_x 并从 setup_axes 方法中调用它。
values_x = [
(3.5, "3.5"), # (position 3.5, label "3.5")
(4.5, "\frac{9}{2}"), # (position 4.5, label "9/2")
]
self.x_axis_labels = VGroup() # Create a group named x_axis_labels
# pos. tex.
for x_val, x_tex in values_x:
tex = TexMobject(x_tex) # Convert string to tex
tex.set_color(self.label_nums_color)
tex.scale(self.x_label_font_size)
tex.next_to(self.coords_to_point(x_val, 0), DOWN) # Put tex on the position
self.x_axis_labels.add(tex) # Add tex in graph