-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy path_subplot.py
More file actions
200 lines (157 loc) · 5.12 KB
/
_subplot.py
File metadata and controls
200 lines (157 loc) · 5.12 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
from typing import Literal, Union
import numpy as np
import pygfx
from rendercanvas import BaseRenderCanvas
from ..graphics import TextGraphic
from ._utils import create_camera, create_controller
from ._plot_area import PlotArea
from ._frame import Frame
from ..graphics._axes import Axes
class Subplot(PlotArea):
def __init__(
self,
parent: Union["Figure"],
camera: Literal["2d", "3d"] | pygfx.PerspectiveCamera,
controller: pygfx.Controller | str,
canvas: BaseRenderCanvas | pygfx.Texture,
rect: np.ndarray = None,
extent: np.ndarray = None,
resizeable: bool = True,
renderer: pygfx.WgpuRenderer = None,
name: str = None,
):
"""
Subplot class.
.. important::
``Subplot`` is not meant to be constructed directly, it only exists as part of a ``Figure``
Parameters
----------
parent: 'Figure' | None
parent Figure instance
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: BaseRenderCanvas, or a pygfx.Texture
Provides surface on which a scene will be rendered.
renderer: WgpuRenderer
object used to render scenes using wgpu
name: str, optional
name of the subplot, will appear as ``TextGraphic`` above the subplot
"""
camera = create_camera(camera)
controller = create_controller(controller_type=controller, camera=camera)
self._docks = dict()
if "Imgui" in parent.__class__.__name__:
toolbar_visible = True
else:
toolbar_visible = False
super().__init__(
parent=parent,
camera=camera,
controller=controller,
scene=pygfx.Scene(),
canvas=canvas,
renderer=renderer,
name=name,
)
for pos in ["left", "top", "right", "bottom"]:
dv = Dock(self, size=0)
dv.name = pos
self.docks[pos] = dv
self.children.append(dv)
self._axes = Axes(self)
self.scene.add(self.axes.world_object)
self._frame = Frame(
viewport=self.viewport,
rect=rect,
extent=extent,
resizeable=resizeable,
title=name,
docks=self.docks,
toolbar_visible=toolbar_visible,
canvas_rect=parent.get_pygfx_render_area(),
)
@property
def axes(self) -> Axes:
"""Axes object"""
return self._axes
@property
def name(self) -> str:
"""Subplot name"""
return self._name
@name.setter
def name(self, name: str):
if name is None:
self._name = None
return
for subplot in self.get_figure(self):
if (subplot is self) or (subplot is None):
continue
if subplot.name == name:
raise ValueError("subplot names must be unique")
self._name = 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
@property
def toolbar(self) -> bool:
"""show/hide toolbar"""
return self.frame.toolbar_visible
@toolbar.setter
def toolbar(self, visible: bool):
self.frame.toolbar_visible = visible
self.frame.reset_viewport()
def _render(self):
self.axes.update_using_camera()
super()._render()
@property
def title(self) -> TextGraphic:
"""subplot title"""
return self._frame.title_graphic
@title.setter
def title(self, text: str):
text = str(text)
self.title.text = text
@property
def frame(self) -> Frame:
"""Frame that the subplot lives in"""
return self._frame
class Dock(PlotArea):
def __init__(
self,
parent: Subplot,
size: int,
):
self._size = size
super().__init__(
parent=parent,
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.get_figure()._fpl_reset_layout()
def _render(self):
if self.size == 0:
return
super()._render()