3b1b/manim · error · IndexError
Index {index} out of bound for matrix with {len(self.columns
Error message
Index {index} out of bound for matrix with {len(self.columns)} columns What it means
IndexError from Matrix.get_column (manimlib/mobject/matrix.py:137) with message 'Index {index} out of bound for matrix with {n} columns'. self.columns is the VGroup of column elements built at construction time from the matrix data; get_column validates 0 <= index < len(self.columns) and raises with the offending index and actual column count so you can see how far off you were.
Source
Thrown at manimlib/mobject/matrix.py:137
else:
return Tex(str(element), **config)
def create_brackets(self, rows, v_buff: float, h_buff: float) -> VGroup:
brackets = Tex("".join((
R"\left[\begin{array}{c}",
*len(rows) * [R"\quad \\"],
R"\end{array}\right]",
)))
brackets.set_height(rows.get_height() + v_buff)
l_bracket = brackets[:len(brackets) // 2]
r_bracket = brackets[len(brackets) // 2:]
l_bracket.next_to(rows, LEFT, h_buff)
r_bracket.next_to(rows, RIGHT, h_buff)
return VGroup(l_bracket, r_bracket)
def get_column(self, index: int):
if not 0 <= index < len(self.columns):
raise IndexError(f"Index {index} out of bound for matrix with {len(self.columns)} columns")
return self.columns[index]
def get_row(self, index: int):
if not 0 <= index < len(self.rows):
raise IndexError(f"Index {index} out of bound for matrix with {len(self.rows)} rows")
return self.rows[index]
def get_columns(self) -> VGroup:
return self.columns
def get_rows(self) -> VGroup:
return self.rows
def set_column_colors(self, *colors: ManimColor) -> Self:
columns = self.get_columns()
for color, column in zip(colors, columns):
column.set_color(color)
return selfView on GitHub (pinned to dee01804d4)
Solutions
- Use 0-based indices strictly less than len(matrix.get_columns())
- Derive bounds from the object: for i in range(len(m.get_columns())): m.get_column(i)
- For the last column use index len(m.get_columns()) - 1, not -1
Example fix
# before m = Matrix([[1, 2], [3, 4]]) m.get_column(2) # raises: Index 2 out of bound ... 2 columns m.get_column(-1) # also raises (no negative indexing) # after m = Matrix([[1, 2], [3, 4]]) last = m.get_column(len(m.get_columns()) - 1)
Defensive patterns
Strategy: validation
Validate before calling
n_cols = len(matrix.get_columns())
assert 0 <= col_index < n_cols, f"col_index must be in [0, {n_cols})"
column = matrix.get_column(col_index) Type guard
def is_valid_column_index(matrix, i: int) -> bool:
return 0 <= i < len(matrix.get_columns()) Prevention
- Use 0-based indices derived from len(matrix.get_columns())
- Remember negative indices are rejected by this API despite being valid elsewhere in Python
- After changing matrix data, re-derive hardcoded column indices
When it happens
Trigger: Matrix([[1,2],[3,4]]).get_column(5); negative indices (get_column(-1) fails this check even though Python slicing would accept it); index computed from user data or loop bounds that exceed the number of columns; matrices constructed via DecimalMatrix/Matrix of a shorter dataset than the index assumes.
Common situations: Coloring specific columns (set_column_colors is safe, but manual get_column(i) loops with wrong bounds); code written against a larger matrix later fed a smaller one; off-by-one loop bounds; assuming 1-based indexing (get_column(1) for the first column is fine, but get_column(n) for the last is not).
Related errors
- Index {index} out of bound for matrix with {len(self.rows)}
- Input to MobjectMatrix must have at least n_rows * n_cols en
- At least 2 mobjects needed for Union.
- At least 2 mobjects needed for Intersection.
- At least 2 mobjects needed for Exclusion.
AI-assisted analysis of 3b1b/manim@dee01804d4 (2026-08-14).
Data as JSON: /api/errors/10c4af724ce55f81.
Report an issue: GitHub.