Create and return a `.SubplotSpec` instance.
(self, key)
| 211 | return GridSpec(nrows, ncols, figure=figure) |
| 212 | |
| 213 | def __getitem__(self, key): |
| 214 | """Create and return a `.SubplotSpec` instance.""" |
| 215 | nrows, ncols = self.get_geometry() |
| 216 | |
| 217 | def _normalize(key, size, axis): # Includes last index. |
| 218 | orig_key = key |
| 219 | if isinstance(key, slice): |
| 220 | start, stop, _ = key.indices(size) |
| 221 | if stop > start: |
| 222 | return start, stop - 1 |
| 223 | raise IndexError("GridSpec slice would result in no space " |
| 224 | "allocated for subplot") |
| 225 | else: |
| 226 | if key < 0: |
| 227 | key = key + size |
| 228 | if 0 <= key < size: |
| 229 | return key, key |
| 230 | elif axis is not None: |
| 231 | raise IndexError(f"index {orig_key} is out of bounds for " |
| 232 | f"axis {axis} with size {size}") |
| 233 | else: # flat index |
| 234 | raise IndexError(f"index {orig_key} is out of bounds for " |
| 235 | f"GridSpec with size {size}") |
| 236 | |
| 237 | if isinstance(key, tuple): |
| 238 | try: |
| 239 | k1, k2 = key |
| 240 | except ValueError as err: |
| 241 | raise ValueError("Unrecognized subplot spec") from err |
| 242 | num1, num2 = np.ravel_multi_index( |
| 243 | [_normalize(k1, nrows, 0), _normalize(k2, ncols, 1)], |
| 244 | (nrows, ncols)) |
| 245 | else: # Single key |
| 246 | num1, num2 = _normalize(key, nrows * ncols, None) |
| 247 | |
| 248 | return SubplotSpec(self, num1, num2) |
| 249 | |
| 250 | def subplots(self, *, sharex=False, sharey=False, squeeze=True, |
| 251 | subplot_kw=None): |
nothing calls this directly
no test coverage detected