Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 52f1ac4

Browse filesBrowse files
authored
Merge pull request #13422 from anntzer/gridspec-out-of-bounds
Clarify IndexError for out-of-bounds indexing of gridspec.
2 parents dbaf19d + b14b1c1 commit 52f1ac4
Copy full SHA for 52f1ac4

File tree

Expand file treeCollapse file tree

1 file changed

+14
-5
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+14
-5
lines changed
Open diff view settings
Collapse file

‎lib/matplotlib/gridspec.py‎

Copy file name to clipboardExpand all lines: lib/matplotlib/gridspec.py
+14-5Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,27 +146,36 @@ def __getitem__(self, key):
146146
"""
147147
nrows, ncols = self.get_geometry()
148148

149-
def _normalize(key, size): # Includes last index.
149+
def _normalize(key, size, axis): # Includes last index.
150+
orig_key = key
150151
if isinstance(key, slice):
151152
start, stop, _ = key.indices(size)
152153
if stop > start:
153154
return start, stop - 1
155+
raise IndexError("GridSpec slice would result in no space "
156+
"allocated for subplot")
154157
else:
155158
if key < 0:
156-
key += size
159+
key = key + size
157160
if 0 <= key < size:
158161
return key, key
159-
raise IndexError("invalid index")
162+
elif axis is not None:
163+
raise IndexError(f"index {orig_key} is out of bounds for "
164+
f"axis {axis} with size {size}")
165+
else: # flat index
166+
raise IndexError(f"index {orig_key} is out of bounds for "
167+
f"GridSpec with size {size}")
160168

161169
if isinstance(key, tuple):
162170
try:
163171
k1, k2 = key
164172
except ValueError:
165173
raise ValueError("unrecognized subplot spec")
166174
num1, num2 = np.ravel_multi_index(
167-
[_normalize(k1, nrows), _normalize(k2, ncols)], (nrows, ncols))
175+
[_normalize(k1, nrows, 0), _normalize(k2, ncols, 1)],
176+
(nrows, ncols))
168177
else: # Single key
169-
num1, num2 = _normalize(key, nrows * ncols)
178+
num1, num2 = _normalize(key, nrows * ncols, None)
170179

171180
return SubplotSpec(self, num1, num2)
172181

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.