Skip to content

Commit 4a13244

Browse files
authored
Figure can accept rects or extents as an dict with keys indicating subplot names (#1014)
* extents or rects can also be an OrderedDict * black * even better, just regular dict
1 parent 57fd4fb commit 4a13244

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

fastplotlib/layouts/_figure.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import os
1+
from inspect import getfullargspec
22
from itertools import product, chain
3+
import os
34
from pathlib import Path
4-
5-
import numpy as np
65
from typing import Literal, Iterable
7-
from inspect import getfullargspec
86
from warnings import warn
97

10-
import pygfx
8+
import numpy as np
119

10+
import pygfx
1211
from rendercanvas import BaseRenderCanvas
1312

1413
from ._utils import (
@@ -27,8 +26,8 @@ class Figure:
2726
def __init__(
2827
self,
2928
shape: tuple[int, int] = (1, 1),
30-
rects: list[tuple | np.ndarray] = None,
31-
extents: list[tuple | np.ndarray] = None,
29+
rects: list[tuple | np.ndarray] | dict[str, tuple | np.ndarray] = None,
30+
extents: list[tuple | np.ndarray] | dict[str, tuple | np.ndarray] = None,
3231
cameras: (
3332
Literal["2d", "3d"]
3433
| Iterable[Iterable[Literal["2d", "3d"]]]
@@ -60,15 +59,17 @@ def __init__(
6059
shape: tuple[int, int], default (1, 1)
6160
shape [n_rows, n_cols] that defines a grid of subplots
6261
63-
rects: list of tuples or arrays
64-
list of rects (x, y, width, height) that define the subplots.
62+
rects: list of tuples or arrays, or a dict mapping subplot name -> rect
63+
list or dict of rects (x, y, width, height) that define the subplots.
64+
If it is a dict, the keys are used as the subplot names.
6565
rects can be defined in absolute pixels or as a fraction of the canvas.
6666
If width & height <= 1 the rect is assumed to be fractional.
6767
Conversely, if width & height > 1 the rect is assumed to be in absolute pixels.
6868
width & height must be > 0. Negative values are not allowed.
6969
70-
extents: list of tuples or arrays
71-
list of extents (xmin, xmax, ymin, ymax) that define the subplots.
70+
extents: list of tuples or arrays, or a dict mapping subplot name -> extent
71+
list or dict of extents (xmin, xmax, ymin, ymax) that define the subplots.
72+
If it is a dict, the keys are used as the subplot names.
7273
extents can be defined in absolute pixels or as a fraction of the canvas.
7374
If xmax & ymax <= 1 the extent is assumed to be fractional.
7475
Conversely, if xmax & ymax > 1 the extent is assumed to be in absolute pixels.
@@ -120,7 +121,7 @@ def __init__(
120121
starting size of canvas in absolute pixels, default (500, 300)
121122
122123
names: list or array of str, optional
123-
subplot names
124+
subplot names, ignored if extents or rects are provided as a dict
124125
125126
"""
126127
# create canvas and renderer
@@ -148,6 +149,10 @@ def __init__(
148149
self._fpl_overlay_scene = pygfx.Scene()
149150

150151
if rects is not None:
152+
if isinstance(rects, dict):
153+
# the actual rects are the dict values, subplot names are the keys
154+
names, rects = zip(*rects.items())
155+
151156
if not all(isinstance(v, (np.ndarray, tuple, list)) for v in rects):
152157
raise TypeError(
153158
f"rects must a list of arrays, tuples, or lists of rects (x, y, w, h), you have passed: {rects}"
@@ -157,6 +162,10 @@ def __init__(
157162
extents = [None] * n_subplots
158163

159164
elif extents is not None:
165+
if isinstance(extents, dict):
166+
# the actual extents are the dict values, subplot names are the keys
167+
names, extents = zip(*extents.items())
168+
160169
if not all(isinstance(v, (np.ndarray, tuple, list)) for v in extents):
161170
raise TypeError(
162171
f"extents must a list of arrays, tuples, or lists of extents (xmin, xmax, ymin, ymax), "

0 commit comments

Comments
 (0)