matplotlib/matplotlib · error · TypeError
col must be an int or sequence of ints.
Error message
col must be an int or sequence of ints.
What it means
Table.auto_set_column_width wraps its argument with np.atleast_1d and requires the resulting dtype to be integer (table.py:508). A single float, a list of floats, string digits, or a float numpy array raises TypeError — even values like 1.0 that look like valid column indices.
Source
Thrown at lib/matplotlib/table.py:508
ypos += heights[row]
# set cell positions
for (row, col), cell in self._cells.items():
cell.set_x(lefts[col])
cell.set_y(bottoms[row])
def auto_set_column_width(self, col):
"""
Automatically set the widths of given columns to optimal sizes.
Parameters
----------
col : int or sequence of ints
The indices of the columns to auto-scale.
"""
col1d = np.atleast_1d(col)
if not np.issubdtype(col1d.dtype, np.integer):
raise TypeError("col must be an int or sequence of ints.")
for cell in col1d:
self._autoColumns.append(cell)
self.stale = True
def _auto_set_column_width(self, col, renderer):
"""Automatically set width for column."""
cells = [cell for key, cell in self._cells.items() if key[1] == col]
max_width = max((cell.get_required_width(renderer) for cell in cells),
default=0)
for cell in cells:
cell.set_width(max_width)
def auto_set_font_size(self, value=True):
"""Automatically set font size."""
self._autoFontsize = value
self.stale = True
View on GitHub (pinned to b379c1b69e)
Solutions
- Pass ints: table.auto_set_column_width(0) or table.auto_set_column_width([0, 2])
- Coerce untrusted input first: table.auto_set_column_width([int(c) for c in cols])
- Generate candidate columns with np.arange(n), never np.linspace
Example fix
# before: float indices from a config file auto_cols = [0.0, 2.0] table.auto_set_column_width(auto_cols) # TypeError: col must be an int or sequence of ints # after: coerce to Python ints table.auto_set_column_width([int(c) for c in auto_cols])
Defensive patterns
Strategy: type-guard
Validate before calling
import numpy as np
def as_int_columns(col):
a = np.atleast_1d(col)
if not np.issubdtype(a.dtype, np.integer):
if np.issubdtype(a.dtype, np.floating) and np.all(a == a.astype(int)):
a = a.astype(int)
else:
raise TypeError(f'column indices must be ints, got dtype {a.dtype}')
return a.tolist()
table.auto_set_column_width(as_int_columns(cfg['auto_cols'])) Type guard
import numpy as np
def is_int_index_list(col) -> bool:
try:
a = np.atleast_1d(col)
except Exception:
return False
return np.issubdtype(a.dtype, np.integer) Try / catch
try:
table.auto_set_column_width(col)
except TypeError as e:
raise TypeError(f'auto_set_column_width needs int indices, got {col!r}') from e Prevention
- Int-coerce config values at load: json ints stay ints, yaml floats get int(v)
- Use range(n)/np.arange(n) when generating candidate columns
- Never derive column indices from division or linspace results
When it happens
Trigger: table.auto_set_column_width(0.0); auto_set_column_width(np.array([0.0, 1.0])); auto_set_column_width(['0', '1']); column indices computed from divisions or read from JSON as floats.
Common situations: Indices derived from arithmetic (i / 2, np.linspace) or config/JSON parsing that yields floats; pandas .get_loc results assumed to be ints on categorical indexes (they can be slices/booleans).
Understand the failure class
Background: Invalid argument type errors: "must be of type string", "expected X, got Y", and ERR_INVALID_ARG_TYPE explained — this error's family across 15 libraries.
Related errors
- Image data of dtype {A.dtype} cannot be converted to float
- 'slope' cannot be used with non-linear scales
- the dtypes of parameters x ({x.dtype}) and width ({width.dty
- the dtypes of parameters y ({y.dtype}) and height ({height.d
- s must be float, but has type {s.dtype}
AI-assisted analysis of matplotlib/matplotlib@b379c1b69e (2026-08-21).
Data as JSON: /api/errors/36c4bcc7dd4e3cdc.
Report an issue: GitHub.