Skip to content

Bug: IndexError (shape mismatch) when using 2D slicing on SubplotGrid #434

@syrte

Description

@syrte

Description
I encountered an IndexError when attempting to slice the axs object returned by uplt.subplots. It appears ultraplot fails to handle 2D slices where the dimensions differ (e.g., [:, :2]), raising a shape mismatch error during the internal indexing process.

Minimal Reproduction

import ultraplot as uplt
import matplotlib as mpl

print(f"ultraplot: {uplt.__version__}")
print(f"matplotlib: {mpl.__version__}")

fig, axs = uplt.subplots(nrows=3, ncols=3)

# This 2D slice triggers the shape mismatch error
axs[:, :2].format(ylim=(-1, 1))

Traceback

IndexError                                Traceback (most recent call last)
Cell In[62], line 6
      4 print(mpl.__version__)
      5 fig, axs = uplt.subplots(nrows=3, ncols=3)
----> 6 axs[:, :2].format(ylim=(-1, 1))

File ~/conda/envs/python/lib/python3.13/site-packages/ultraplot/gridspec.py:1653, in SubplotGrid.__getitem__(self, key)
   1651     new_key.append(encoded_keyi)
   1652 xs, ys = new_key
-> 1653 objs = grid[xs, ys]
   1654 if hasattr(objs, "flat"):
   1655     objs = [obj for obj in objs.flat if obj is not None]

IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes (3,) (2,)

Analysis (by gemini if relevant)
The error shape mismatch: indexing arrays could not be broadcast together with shapes (3,) (2,) suggests that SubplotGrid.__getitem__ is internally converting the slices into integer arrays but attempting to use them for direct advanced indexing without creating a meshgrid (or using np.ix_).

When slicing [:, :2] on a 3x3 grid, it generates row indices of shape (3,) and column indices of shape (2,), which causes a broadcast failure in numpy if not handled as a cross-product.

Environment

  • ultraplot version: 1.66.0
  • matplotlib version: 3.9.4
  • Python version: 3.13

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions