Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion ultraplot/gridspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -1988,9 +1988,11 @@ def __getitem__(self, key):
elif not isinstance(objs, list):
objs = [objs]

# Spanning subplots can appear more than once in the sliced slot grid.
# De-duplicate while preserving order so method dispatch does not repeat.
objs = list(dict.fromkeys(objs))
if len(objs) == 1:
return objs[0]
objs = [obj for obj in objs if obj is not None]
return SubplotGrid(objs)

def __setitem__(self, key, value):
Expand Down
19 changes: 19 additions & 0 deletions ultraplot/tests/test_gridspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,22 @@ def test_gridspec_slicing():
# subset_mixed[4] -> Row 1, Col 0 -> Number 5 (since 4 cols per row)
assert subset_mixed[0].number == 1
assert subset_mixed[4].number == 5


def test_gridspec_spanning_slice_deduplicates_axes():
import numpy as np

fig, axs = uplt.subplots(np.array([[1, 1, 2], [3, 4, 5]]))

# The first two slots in the top row refer to the same spanning subplot.
ax = axs[0, :2]
assert isinstance(ax, uplt.axes.Axes)
assert ax is axs[0, 0]

data = np.array([[0.1, 0.2], [0.4, 0.5], [0.7, 0.8]])
ax.scatter(data[:, 0], data[:, 1], c="grey", label="data", legend=True)
fig.canvas.draw()

legend = ax.get_legend()
assert legend is not None
assert [t.get_text() for t in legend.texts] == ["data"]