-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy path_subplot.py
More file actions
351 lines (281 loc) · 11.1 KB
/
_subplot.py
File metadata and controls
351 lines (281 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
from typing import *
import numpy as np
import pygfx
from wgpu.gui.auto import WgpuCanvas
from ..graphics import TextGraphic
from ._utils import make_canvas_and_renderer, create_camera, create_controller
from ._plot_area import PlotArea
from .graphic_methods_mixin import GraphicMethodsMixin
class Subplot(PlotArea, GraphicMethodsMixin):
def __init__(
self,
parent: Any = None,
position: Tuple[int, int] = None,
parent_dims: Tuple[int, int] = None,
camera: Union[str, pygfx.PerspectiveCamera] = "2d",
controller: Union[str, pygfx.Controller] = None,
canvas: Union[str, WgpuCanvas, pygfx.Texture] = None,
renderer: pygfx.WgpuRenderer = None,
name: str = None,
):
"""
General plot object that composes a ``Gridplot``. Each ``Gridplot`` instance will have [n rows, n columns]
of subplots.
.. important::
``Subplot`` is not meant to be constructed directly, it only exists as part of a ``GridPlot``
Parameters
----------
parent: Any
parent GridPlot instance
position: (int, int), optional
corresponds to the [row, column] position of the subplot within a ``Gridplot``
parent_dims: (int, int), optional
dimensions of the parent ``GridPlot``
camera: str or pygfx.PerspectiveCamera, default '2d'
indicates the FOV for the camera, '2d' sets ``fov = 0``, '3d' sets ``fov = 50``.
``fov`` can be changed at any time.
controller: str or pygfx.Controller, optional
| if ``None``, uses a PanZoomController for "2d" camera or FlyController for "3d" camera.
| if ``str``, must be one of: `"panzoom", "fly", "trackball", or "orbit"`.
| also accepts a pygfx.Controller instance
canvas: one of "jupyter", "glfw", "qt", WgpuCanvas, or pygfx.Texture, optional
Provides surface on which a scene will be rendered. Can optionally provide a WgpuCanvas instance or a str
to force the PlotArea to use a specific canvas from one of the following options: "jupyter", "glfw", "qt".
Can also provide a pygfx Texture to render to.
renderer: WgpuRenderer, optional
object used to render scenes using wgpu
name: str, optional
name of the subplot, will appear as ``TextGraphic`` above the subplot
"""
super(GraphicMethodsMixin, self).__init__()
canvas, renderer = make_canvas_and_renderer(canvas, renderer)
if position is None:
position = (0, 0)
if parent_dims is None:
parent_dims = (1, 1)
self.nrows, self.ncols = parent_dims
camera = create_camera(camera)
controller = create_controller(controller_type=controller, camera=camera)
self._docks = dict()
self.spacing = 2
self._axes: pygfx.AxesHelper = pygfx.AxesHelper(size=100)
for arrow in self._axes.children:
self._axes.remove(arrow)
self._grid: pygfx.GridHelper = pygfx.GridHelper(size=100, thickness=1)
super(Subplot, self).__init__(
parent=parent,
position=position,
camera=camera,
controller=controller,
scene=pygfx.Scene(),
canvas=canvas,
renderer=renderer,
name=name,
)
for pos in ["left", "top", "right", "bottom"]:
dv = Dock(self, pos, size=0)
dv.name = pos
self.docks[pos] = dv
self.children.append(dv)
self._title_graphic: TextGraphic = None
if self.name is not None:
self.set_title(self.name)
@property
def name(self) -> Any:
return self._name
@name.setter
def name(self, name: Any):
self._name = name
self.set_title(name)
@property
def docks(self) -> dict:
"""
The docks of this plot area. Each ``dock`` is basically just a PlotArea too.
The docks are: ["left", "top", "right", "bottom"]
Returns
-------
Dict[str, Dock]
{dock_name: Dock}
"""
return self._docks
def set_title(self, text: Any):
"""Sets the plot title, stored as a ``TextGraphic`` in the "top" dock area"""
if text is None:
return
text = str(text)
if self._title_graphic is not None:
self._title_graphic.text = text
else:
tg = TextGraphic(text=text, size=18)
self._title_graphic = tg
self.docks["top"].size = 35
self.docks["top"].add_graphic(tg)
self.center_title()
def center_title(self):
"""Centers name of subplot."""
if self._title_graphic is None:
raise AttributeError("No title graphic is set")
self._title_graphic.world_object.position = (0, 0, 0)
self.docks["top"].center_graphic(self._title_graphic, zoom=1.5)
self._title_graphic.world_object.position_y = -3.5
def get_rect(self):
"""Returns the bounding box that defines the Subplot within the canvas."""
row_ix, col_ix = self.position
width_canvas, height_canvas = self.renderer.logical_size
x_pos = (
(width_canvas / self.ncols) + ((col_ix - 1) * (width_canvas / self.ncols))
) + self.spacing
y_pos = (
(height_canvas / self.nrows) + ((row_ix - 1) * (height_canvas / self.nrows))
) + self.spacing
width_subplot = (width_canvas / self.ncols) - self.spacing
height_subplot = (height_canvas / self.nrows) - self.spacing
rect = np.array([x_pos, y_pos, width_subplot, height_subplot])
for dv in self.docks.values():
rect = rect + dv.get_parent_rect_adjust()
return rect
def set_axes_visibility(self, visible: bool):
"""Toggles axes visibility."""
if visible:
self.scene.add(self._axes)
else:
self.scene.remove(self._axes)
def set_grid_visibility(self, visible: bool):
"""Toggles grid visibility."""
if visible:
self.scene.add(self._grid)
else:
self.scene.remove(self._grid)
class Dock(PlotArea):
_valid_positions = ["right", "left", "top", "bottom"]
def __init__(
self,
parent: Subplot,
position: str,
size: int,
):
if position not in self._valid_positions:
raise ValueError(
f"the `position` of an AnchoredViewport must be one of: {self._valid_positions}"
)
self._size = size
super(Dock, self).__init__(
parent=parent,
position=position,
camera=pygfx.OrthographicCamera(),
controller=pygfx.PanZoomController(),
scene=pygfx.Scene(),
canvas=parent.canvas,
renderer=parent.renderer,
)
@property
def size(self) -> int:
"""Get or set the size of this dock"""
return self._size
@size.setter
def size(self, s: int):
self._size = s
self.parent.set_viewport_rect()
self.set_viewport_rect()
def get_rect(self, *args):
if self.size == 0:
self.viewport.rect = None
return
row_ix_parent, col_ix_parent = self.parent.position
width_canvas, height_canvas = self.parent.renderer.logical_size
spacing = 2 # spacing in pixels
if self.position == "right":
x_pos = (
(width_canvas / self.parent.ncols)
+ ((col_ix_parent - 1) * (width_canvas / self.parent.ncols))
+ (width_canvas / self.parent.ncols)
- self.size
)
y_pos = (
(height_canvas / self.parent.nrows)
+ ((row_ix_parent - 1) * (height_canvas / self.parent.nrows))
) + spacing
width_viewport = self.size
height_viewport = (height_canvas / self.parent.nrows) - spacing
elif self.position == "left":
x_pos = (width_canvas / self.parent.ncols) + (
(col_ix_parent - 1) * (width_canvas / self.parent.ncols)
)
y_pos = (
(height_canvas / self.parent.nrows)
+ ((row_ix_parent - 1) * (height_canvas / self.parent.nrows))
) + spacing
width_viewport = self.size
height_viewport = (height_canvas / self.parent.nrows) - spacing
elif self.position == "top":
x_pos = (
(width_canvas / self.parent.ncols)
+ ((col_ix_parent - 1) * (width_canvas / self.parent.ncols))
+ spacing
)
y_pos = (
(height_canvas / self.parent.nrows)
+ ((row_ix_parent - 1) * (height_canvas / self.parent.nrows))
) + spacing
width_viewport = (width_canvas / self.parent.ncols) - spacing
height_viewport = self.size
elif self.position == "bottom":
x_pos = (
(width_canvas / self.parent.ncols)
+ ((col_ix_parent - 1) * (width_canvas / self.parent.ncols))
+ spacing
)
y_pos = (
(
(height_canvas / self.parent.nrows)
+ ((row_ix_parent - 1) * (height_canvas / self.parent.nrows))
)
+ (height_canvas / self.parent.nrows)
- self.size
)
width_viewport = (width_canvas / self.parent.ncols) - spacing
height_viewport = self.size
else:
raise ValueError("invalid position")
return [x_pos, y_pos, width_viewport, height_viewport]
def get_parent_rect_adjust(self):
if self.position == "right":
return np.array(
[
0, # parent subplot x-position is same
0,
-self.size, # width of parent subplot is `self.size` smaller
0,
]
)
elif self.position == "left":
return np.array(
[
self.size, # `self.size` added to parent subplot x-position
0,
-self.size, # width of parent subplot is `self.size` smaller
0,
]
)
elif self.position == "top":
return np.array(
[
0,
self.size, # `self.size` added to parent subplot y-position
0,
-self.size, # height of parent subplot is `self.size` smaller
]
)
elif self.position == "bottom":
return np.array(
[
0,
0, # parent subplot y-position is same,
0,
-self.size, # height of parent subplot is `self.size` smaller
]
)
def render(self):
if self.size == 0:
return
super(Dock, self).render()