fix: throughput comparison layout — centered rows with proper Left-Right alignment

This commit is contained in:
ViperEkura 2026-05-06 22:02:20 +08:00
parent 4ed16a70b4
commit 42c1d5f700
1 changed files with 51 additions and 58 deletions

View File

@ -265,69 +265,62 @@ class ContinuousBatching(Scene):
self.wait(0.3)
# bars
bar_base = LEFT * 2
bar_max_w = 4.5
bar_h = 0.5
static_bar = Rectangle(width=0, height=bar_h, color=RED,
fill_opacity=0.7, stroke_width=0).move_to(bar_base, LEFT)
static_value = always_redraw(
lambda: DecimalNumber(
static_bar.get_width() / bar_max_w * 1.0,
num_decimal_places=1, font_size=24, color=WHITE,
).next_to(static_bar, RIGHT, buff=0.2)
)
static_rect = Rectangle(width=bar_max_w, height=bar_h, color=RED, stroke_width=1.2)
static_rect.move_to(bar_base, LEFT).shift(RIGHT * bar_max_w / 2)
cb_bar = Rectangle(width=0, height=bar_h, color=GREEN,
fill_opacity=0.7, stroke_width=0).move_to(bar_base, LEFT)
cb_value = always_redraw(
lambda: DecimalNumber(
cb_bar.get_width() / bar_max_w * 3.4,
num_decimal_places=1, font_size=24, color=WHITE,
).next_to(cb_bar, RIGHT, buff=0.2)
)
cb_rect = Rectangle(width=bar_max_w, height=bar_h, color=GREEN, stroke_width=1.2)
cb_rect.move_to(bar_base, LEFT).shift(RIGHT * bar_max_w / 2)
static_label = Text("Static Batching ", font_size=22, color=RED) \
.next_to(static_rect, LEFT, buff=0.3)
cb_label = Text("Continuous Batching", font_size=22, color=GREEN) \
.next_to(cb_rect, LEFT, buff=0.3)
labels = VGroup(static_label, cb_label).arrange(DOWN, buff=0.7, aligned_edge=LEFT)
labels.shift(LEFT * 2)
bar_group = VGroup(
static_rect, static_bar, static_value,
cb_rect, cb_bar, cb_value,
static_label, cb_label,
)
compare_title = Text("Throughput", font_size=30, color=BLUE).next_to(bar_group, UP, buff=0.6)
# ---- title ----
compare_title = Text("Throughput Comparison", font_size=30, color=BLUE)
self.play(Write(compare_title))
self.play(Create(static_rect), Create(cb_rect),
Write(static_label), Write(cb_label))
self.wait(0.2)
self.play(compare_title.animate.to_edge(UP).scale(0.55))
self.wait(0.2)
# grow static bar
self.play(GrowFromEdge(static_bar, LEFT),
rate_func=linear, run_time=0.6)
# ---- bar config ----
bar_max_w = 4.5
bar_h = 0.55
row_gap = 0.7
# Static Batching row
s_label = Text("Static Batching", font_size=24, color=RED)
s_rect = Rectangle(width=bar_max_w, height=bar_h, color=RED, stroke_width=1.5)
s_bar = Rectangle(width=0, height=bar_h, color=RED,
fill_opacity=0.55, stroke_width=0) \
.align_to(s_rect, LEFT).align_to(s_rect, UP)
s_num = Text("1.0x", font_size=24, color=RED)
s_row = VGroup(s_label, s_rect, s_bar, s_num)
s_label.next_to(s_rect, LEFT, buff=0.4)
s_num.next_to(s_rect, RIGHT, buff=0.4)
# Continuous Batching row
c_label = Text("Continuous Batching", font_size=24, color=GREEN)
c_rect = Rectangle(width=bar_max_w, height=bar_h, color=GREEN, stroke_width=1.5)
c_bar = Rectangle(width=0, height=bar_h, color=GREEN,
fill_opacity=0.55, stroke_width=0) \
.align_to(c_rect, LEFT).align_to(c_rect, UP)
c_num = Text("3.4x", font_size=24, color=GREEN)
c_row = VGroup(c_label, c_rect, c_bar, c_num)
c_label.next_to(c_rect, LEFT, buff=0.4)
c_num.next_to(c_rect, RIGHT, buff=0.4)
# stack rows, centre
chart = VGroup(s_row, c_row).arrange(DOWN, buff=row_gap, aligned_edge=LEFT)
chart.move_to(ORIGIN).shift(DOWN * 0.25)
self.play(
Create(s_rect), Create(c_rect),
Write(s_label), Write(c_label),
)
self.wait(0.3)
# grow cb bar significantly faster
self.play(GrowFromEdge(cb_bar, LEFT),
rate_func=linear, run_time=0.6)
self.wait(0.4)
# grow static bar
self.play(GrowFromEdge(s_bar, LEFT), rate_func=linear, run_time=0.6)
self.wait(0.3)
# labels under bars
static_num = Text("1.0x", font_size=22, color=RED) \
.next_to(static_rect, RIGHT, buff=0.3)
cb_num = Text("3.4x", font_size=22, color=GREEN) \
.next_to(cb_rect, RIGHT, buff=0.3)
self.play(Write(static_num), Write(cb_num))
# grow cb bar
self.play(GrowFromEdge(c_bar, LEFT), rate_func=linear, run_time=0.6)
self.wait(0.3)
# show values
self.play(Write(s_num), Write(c_num))
self.wait(2.5)
self.play(*[FadeOut(m) for m in self.mobjects])