cogmatplot 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cogmatplot/__init__.py +46 -0
- cogmatplot/api.py +43 -0
- cogmatplot/client.py +151 -0
- cogmatplot/debug_views.py +222 -0
- cogmatplot/images.py +54 -0
- cogmatplot/layout.py +88 -0
- cogmatplot/matplot.py +967 -0
- cogmatplot/mode.py +77 -0
- cogmatplot/place.py +65 -0
- cogmatplot/ply.py +325 -0
- cogmatplot/server.py +124 -0
- cogmatplot/view3.py +353 -0
- cogmatplot/wire.py +93 -0
- cogmatplot-0.1.0.dist-info/METADATA +217 -0
- cogmatplot-0.1.0.dist-info/RECORD +19 -0
- cogmatplot-0.1.0.dist-info/WHEEL +5 -0
- cogmatplot-0.1.0.dist-info/entry_points.txt +2 -0
- cogmatplot-0.1.0.dist-info/licenses/LICENSE +21 -0
- cogmatplot-0.1.0.dist-info/top_level.txt +1 -0
cogmatplot/matplot.py
ADDED
|
@@ -0,0 +1,967 @@
|
|
|
1
|
+
"""MATLAB-style plotting helpers and :class:`FigureManager`.
|
|
2
|
+
|
|
3
|
+
In-process::
|
|
4
|
+
|
|
5
|
+
fm = FigureManager(2, 2, mon=0, pause=False)
|
|
6
|
+
fm.imshow(0, 0, image)
|
|
7
|
+
fm.plot(0, 1, x, y, '.-')
|
|
8
|
+
fm.plot3(1, 0, x, y, z)
|
|
9
|
+
cloud = fm.figure3(1, 1, title='points')
|
|
10
|
+
cloud.scatter3(pts, colors=rgb)
|
|
11
|
+
cloud.scatter3(tag, color='red')
|
|
12
|
+
cloud.view_xy()
|
|
13
|
+
|
|
14
|
+
``mode='inline'`` draws that grid into the notebook as static figures.
|
|
15
|
+
``mode='qt'`` opens an interactive window per tile on ``mon``.
|
|
16
|
+
``FigureManager.connect(host, port)`` sends the same calls to a server.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
import warnings
|
|
22
|
+
import weakref
|
|
23
|
+
|
|
24
|
+
import matplotlib.pyplot as plt
|
|
25
|
+
import numpy as np
|
|
26
|
+
from matplotlib.patches import Rectangle
|
|
27
|
+
|
|
28
|
+
from cogmatplot.images import prepare_bgr
|
|
29
|
+
from cogmatplot.layout import gridfig_box
|
|
30
|
+
from cogmatplot.mode import current_mode
|
|
31
|
+
from cogmatplot.view3 import coerce_cloud
|
|
32
|
+
|
|
33
|
+
_MANAGERS = weakref.WeakSet()
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _as_on(state) -> bool:
|
|
37
|
+
if isinstance(state, str):
|
|
38
|
+
return state.lower() in ("on", "true", "1", "yes")
|
|
39
|
+
return bool(state)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def saveFig(fig, filename):
|
|
43
|
+
fig.patch.set_facecolor("white")
|
|
44
|
+
fig.patch.set_alpha(1.0)
|
|
45
|
+
fig.savefig(filename)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def closeAll():
|
|
49
|
+
"""Close every figure this process opened. MATLAB ``close all``."""
|
|
50
|
+
plt.ioff()
|
|
51
|
+
for manager in list(_MANAGERS):
|
|
52
|
+
manager.closeAll()
|
|
53
|
+
plt.close("all")
|
|
54
|
+
try:
|
|
55
|
+
import cv2
|
|
56
|
+
|
|
57
|
+
cv2.destroyAllWindows()
|
|
58
|
+
except Exception:
|
|
59
|
+
pass
|
|
60
|
+
try:
|
|
61
|
+
import pyvista as pv
|
|
62
|
+
|
|
63
|
+
pv.close_all()
|
|
64
|
+
except Exception:
|
|
65
|
+
pass
|
|
66
|
+
if current_mode() == "qt":
|
|
67
|
+
plt.ion()
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def clfAll():
|
|
71
|
+
"""Clear every open axes. MATLAB ``clf`` across figures."""
|
|
72
|
+
for manager in list(_MANAGERS):
|
|
73
|
+
manager.clfAll()
|
|
74
|
+
for num in list(plt.get_fignums()):
|
|
75
|
+
for ax in plt.figure(num).axes:
|
|
76
|
+
ax.cla()
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def figure(**kwargs):
|
|
80
|
+
fig, ax = plt.subplots(**kwargs)
|
|
81
|
+
return fig, ax
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _place_window(fig, x, y, w, h):
|
|
85
|
+
manager = getattr(fig.canvas, "manager", None)
|
|
86
|
+
window = getattr(manager, "window", None)
|
|
87
|
+
if window is None:
|
|
88
|
+
return
|
|
89
|
+
from cogmatplot.place import place_qt_window
|
|
90
|
+
|
|
91
|
+
place_qt_window(window, x, y, w, h)
|
|
92
|
+
for attr in ("header_visible", "footer_visible"):
|
|
93
|
+
if hasattr(fig.canvas, attr):
|
|
94
|
+
setattr(fig.canvas, attr, False)
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def gridfig(row=0, col=0, rows=3, cols=3, mon=0, colpad=50, rowpad=50, rowend=35, barpad=30, bar=False):
|
|
98
|
+
"""One matplotlib window placed in a monitor grid."""
|
|
99
|
+
x, y, w, h = gridfig_box(row, col, rows, cols, mon, colpad=colpad, rowpad=rowpad, rowend=rowend, barpad=barpad)
|
|
100
|
+
prev = plt.rcParams.get("toolbar")
|
|
101
|
+
plt.rcParams["toolbar"] = "toolbar2" if bar else "None"
|
|
102
|
+
try:
|
|
103
|
+
fig, ax = figure()
|
|
104
|
+
finally:
|
|
105
|
+
if prev is not None:
|
|
106
|
+
plt.rcParams["toolbar"] = prev
|
|
107
|
+
_place_window(fig, x, y, w, h)
|
|
108
|
+
try:
|
|
109
|
+
fig.tight_layout()
|
|
110
|
+
except Exception:
|
|
111
|
+
pass
|
|
112
|
+
_flush_fig(fig)
|
|
113
|
+
return fig, ax
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def fit(fig):
|
|
117
|
+
fig.tight_layout()
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def axfit(ax):
|
|
121
|
+
ax.get_figure().tight_layout()
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def _flush_fig(fig):
|
|
125
|
+
if fig is None:
|
|
126
|
+
return
|
|
127
|
+
try:
|
|
128
|
+
fig.canvas.draw_idle()
|
|
129
|
+
fig.canvas.flush_events()
|
|
130
|
+
except Exception:
|
|
131
|
+
pass
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def _display_inline(fig):
|
|
135
|
+
if fig is None or not getattr(fig, "_cog_inline", False):
|
|
136
|
+
return
|
|
137
|
+
try:
|
|
138
|
+
fig.tight_layout()
|
|
139
|
+
except Exception:
|
|
140
|
+
pass
|
|
141
|
+
try:
|
|
142
|
+
from IPython import get_ipython
|
|
143
|
+
from IPython.display import display, update_display
|
|
144
|
+
|
|
145
|
+
if get_ipython() is None:
|
|
146
|
+
raise RuntimeError("no ipython")
|
|
147
|
+
except Exception:
|
|
148
|
+
_flush_fig(fig)
|
|
149
|
+
return
|
|
150
|
+
did = f"cogmatplot-{id(fig)}"
|
|
151
|
+
seen = getattr(_display_inline, "_seen", set())
|
|
152
|
+
if did in seen:
|
|
153
|
+
update_display(fig, display_id=did)
|
|
154
|
+
else:
|
|
155
|
+
display(fig, display_id=did)
|
|
156
|
+
seen.add(did)
|
|
157
|
+
_display_inline._seen = seen
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def _cv_alive():
|
|
161
|
+
for manager in list(_MANAGERS):
|
|
162
|
+
for cell in manager._cells.values():
|
|
163
|
+
if cell.cv_name:
|
|
164
|
+
return True
|
|
165
|
+
try:
|
|
166
|
+
from cogmatplot.debug_views import get_debug_views
|
|
167
|
+
|
|
168
|
+
view = get_debug_views()
|
|
169
|
+
return view is not None and bool(view.feed_names())
|
|
170
|
+
except Exception:
|
|
171
|
+
return False
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def _pump_cv():
|
|
175
|
+
if not _cv_alive():
|
|
176
|
+
return
|
|
177
|
+
import cv2
|
|
178
|
+
|
|
179
|
+
key = cv2.waitKey(1)
|
|
180
|
+
if key in (-1, 255) or key is None:
|
|
181
|
+
return
|
|
182
|
+
event = {"type": "key", "key": int(key) & 0xFF, "source": "cv"}
|
|
183
|
+
for manager in list(_MANAGERS):
|
|
184
|
+
manager._emit(event)
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def _pump_qt():
|
|
188
|
+
if not any(getattr(manager, "mode", "") == "qt" for manager in list(_MANAGERS)):
|
|
189
|
+
return
|
|
190
|
+
try:
|
|
191
|
+
from PyQt5.QtWidgets import QApplication
|
|
192
|
+
except Exception:
|
|
193
|
+
return
|
|
194
|
+
app = QApplication.instance()
|
|
195
|
+
if app is not None:
|
|
196
|
+
app.processEvents()
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def drawnow(ax=None):
|
|
200
|
+
"""Flush open figures. MATLAB ``drawnow``.
|
|
201
|
+
|
|
202
|
+
With an axes, flush that figure. With no argument, flush every manager.
|
|
203
|
+
Also pumps OpenCV and Qt so mixed tiles stay responsive.
|
|
204
|
+
"""
|
|
205
|
+
if ax is not None:
|
|
206
|
+
fig = ax.get_figure()
|
|
207
|
+
_flush_fig(fig)
|
|
208
|
+
_display_inline(fig)
|
|
209
|
+
else:
|
|
210
|
+
for manager in list(_MANAGERS):
|
|
211
|
+
manager._present()
|
|
212
|
+
for num in list(plt.get_fignums()):
|
|
213
|
+
_flush_fig(plt.figure(num))
|
|
214
|
+
_pump_cv()
|
|
215
|
+
_pump_qt()
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
_drawnow = drawnow
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def labels(ax, title=None, x=None, y=None):
|
|
222
|
+
ax.set_title(title)
|
|
223
|
+
ax.set_xlabel(x)
|
|
224
|
+
ax.set_ylabel(y)
|
|
225
|
+
fit(ax.get_figure())
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def imshow(ax, im, cmap="jet", vmin=None, vmax=None, cbar=False, **kwargs):
|
|
229
|
+
artist = ax.imshow(im, cmap=cmap, vmin=vmin, vmax=vmax, **kwargs)
|
|
230
|
+
if cbar:
|
|
231
|
+
ax.get_figure().colorbar(artist, ax=ax)
|
|
232
|
+
return artist
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
_show_image = imshow
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def imagesc(ax, im, **kwargs):
|
|
239
|
+
"""Scaled image. MATLAB ``imagesc``."""
|
|
240
|
+
data = np.asarray(im, dtype=float)
|
|
241
|
+
finite = data[np.isfinite(data)]
|
|
242
|
+
if finite.size == 0:
|
|
243
|
+
lo, hi = 0.0, 1.0
|
|
244
|
+
else:
|
|
245
|
+
lo, hi = float(finite.min()), float(finite.max())
|
|
246
|
+
kwargs.setdefault("vmin", lo)
|
|
247
|
+
kwargs.setdefault("vmax", hi)
|
|
248
|
+
kwargs.setdefault("cmap", "jet")
|
|
249
|
+
return _show_image(ax, im, **kwargs)
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
def updateImage(h, im, auto=True, flush=True):
|
|
253
|
+
h.set_data(im)
|
|
254
|
+
h.set_extent((0, im.shape[1], im.shape[0], 0))
|
|
255
|
+
h.axes.set_xlim(0, im.shape[1])
|
|
256
|
+
h.axes.set_ylim(im.shape[0], 0)
|
|
257
|
+
if auto:
|
|
258
|
+
h.autoscale()
|
|
259
|
+
if flush:
|
|
260
|
+
_flush_fig(h.get_figure())
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
def colorbar(h):
|
|
264
|
+
return h.axes.get_figure().colorbar(h, ax=h.axes)
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
def gridon(ax):
|
|
268
|
+
ax.xaxis.grid(True, which="both")
|
|
269
|
+
ax.yaxis.grid(True, which="both")
|
|
270
|
+
zaxis = getattr(ax, "zaxis", None)
|
|
271
|
+
if zaxis is not None:
|
|
272
|
+
zaxis.grid(True, which="both")
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
def gridoff(ax):
|
|
276
|
+
ax.xaxis.grid(False, which="both")
|
|
277
|
+
ax.yaxis.grid(False, which="both")
|
|
278
|
+
zaxis = getattr(ax, "zaxis", None)
|
|
279
|
+
if zaxis is not None:
|
|
280
|
+
zaxis.grid(False, which="both")
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
def hold(ax, state="on"):
|
|
284
|
+
"""MATLAB ``hold on`` / ``hold off`` for an axes you already have."""
|
|
285
|
+
ax._cog_hold = _as_on(state)
|
|
286
|
+
return ax
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
class Cell:
|
|
290
|
+
def __init__(self, kind="mpl"):
|
|
291
|
+
self.kind = kind
|
|
292
|
+
self.fig = None
|
|
293
|
+
self.ax = None
|
|
294
|
+
self.image = None
|
|
295
|
+
self.line = None
|
|
296
|
+
self.hold = False
|
|
297
|
+
self.cbar = False
|
|
298
|
+
self.cloud = None
|
|
299
|
+
self.cv_name = None
|
|
300
|
+
self.owns_fig = False
|
|
301
|
+
self.figbar = False
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
def _plot3_points(x, y, z, fmt, colors):
|
|
305
|
+
"""Return an ``(N, 3)`` cloud when this ``plot3`` call is markers, not a line."""
|
|
306
|
+
if y is None and z is None:
|
|
307
|
+
arr = np.asarray(x)
|
|
308
|
+
if getattr(arr, "ndim", 0) == 2 and arr.shape[1] == 3:
|
|
309
|
+
return arr
|
|
310
|
+
return None
|
|
311
|
+
marker = isinstance(fmt, str) and "-" not in fmt and any(ch in fmt for ch in ".o+x*sd^v<>")
|
|
312
|
+
if marker or colors is not None:
|
|
313
|
+
return np.column_stack((np.ravel(np.asarray(x)), np.ravel(np.asarray(y)), np.ravel(np.asarray(z))))
|
|
314
|
+
return None
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
def _draw_mpl_layer(ax, layer):
|
|
318
|
+
if layer.get("kind") == "mesh":
|
|
319
|
+
_mesh_layer(ax, layer)
|
|
320
|
+
else:
|
|
321
|
+
_scatter_layer(ax, layer)
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
def _mesh_layer(ax, layer):
|
|
325
|
+
from mpl_toolkits.mplot3d.art3d import Line3DCollection, Poly3DCollection
|
|
326
|
+
|
|
327
|
+
pts = np.asarray(layer["points"], dtype=float)
|
|
328
|
+
faces = np.asarray(layer["faces"], dtype=int)[:, :3]
|
|
329
|
+
style = str(layer.get("style") or "surface").lower()
|
|
330
|
+
color = layer.get("color") or "lightsteelblue"
|
|
331
|
+
tris = pts[faces]
|
|
332
|
+
if style in ("surface", "both", "shaded"):
|
|
333
|
+
edge = "black" if style == "both" else "none"
|
|
334
|
+
ax.add_collection3d(Poly3DCollection(tris, facecolor=color, edgecolor=edge, linewidths=0.4))
|
|
335
|
+
if style == "wireframe":
|
|
336
|
+
loops = [np.vstack((pts[tri], pts[tri[0]])) for tri in faces]
|
|
337
|
+
ax.add_collection3d(Line3DCollection(loops, colors="black", linewidths=0.8))
|
|
338
|
+
lo, hi = pts.min(axis=0), pts.max(axis=0)
|
|
339
|
+
ax.set_xlim(lo[0], hi[0])
|
|
340
|
+
ax.set_ylim(lo[1], hi[1])
|
|
341
|
+
ax.set_zlim(lo[2], hi[2])
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
def _scatter_layer(ax, layer):
|
|
345
|
+
pts, colors = coerce_cloud(layer["points"], layer["colors"])
|
|
346
|
+
size = layer["point_size"]
|
|
347
|
+
if colors is None:
|
|
348
|
+
ax.scatter(pts[:, 0], pts[:, 1], pts[:, 2], c=layer["color"] or "silver", s=size)
|
|
349
|
+
return
|
|
350
|
+
col = np.asarray(colors)
|
|
351
|
+
if col.ndim == 2 and col.shape[1] == 3:
|
|
352
|
+
rgb = col.astype(float)
|
|
353
|
+
if rgb.size and np.nanmax(rgb) > 1.0:
|
|
354
|
+
rgb /= 255.0
|
|
355
|
+
ax.scatter(pts[:, 0], pts[:, 1], pts[:, 2], c=rgb, s=size)
|
|
356
|
+
return
|
|
357
|
+
kw = {}
|
|
358
|
+
if layer["cmap"] is not None:
|
|
359
|
+
kw["cmap"] = layer["cmap"]
|
|
360
|
+
ax.scatter(pts[:, 0], pts[:, 1], pts[:, 2], c=col.reshape(-1), s=size, **kw)
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
class FigureManager:
|
|
364
|
+
"""Grid of tiles. Each tile is matplotlib, OpenCV, or a 3D point cloud.
|
|
365
|
+
|
|
366
|
+
``FigureManager(rows, cols, mon)`` matches the positional form used in
|
|
367
|
+
notebooks. ``connect`` returns a client with the same method names.
|
|
368
|
+
"""
|
|
369
|
+
|
|
370
|
+
def __init__(self, rows, cols, mon=0, pause=True, mode=None, figsize=None):
|
|
371
|
+
self.rows = max(1, int(rows))
|
|
372
|
+
self.cols = max(1, int(cols))
|
|
373
|
+
self.mon = int(mon)
|
|
374
|
+
self._pause = bool(pause)
|
|
375
|
+
self.mode = mode or current_mode()
|
|
376
|
+
self.figsize = figsize
|
|
377
|
+
self._cells = {}
|
|
378
|
+
self._inline_fig = None
|
|
379
|
+
self._axes = None
|
|
380
|
+
self._on_event = None
|
|
381
|
+
self._cloud_warned = False
|
|
382
|
+
_MANAGERS.add(self)
|
|
383
|
+
|
|
384
|
+
@classmethod
|
|
385
|
+
def connect(cls, host="localhost", port=5555):
|
|
386
|
+
from cogmatplot.client import PlotClient
|
|
387
|
+
|
|
388
|
+
return PlotClient(host, port)
|
|
389
|
+
|
|
390
|
+
def _is_inline(self):
|
|
391
|
+
return self.mode == "inline"
|
|
392
|
+
|
|
393
|
+
def _rc(self, row, col):
|
|
394
|
+
row = int(max(0, min(self.rows - 1, int(row))))
|
|
395
|
+
col = int(max(0, min(self.cols - 1, int(col))))
|
|
396
|
+
return row, col
|
|
397
|
+
|
|
398
|
+
def _emit(self, event):
|
|
399
|
+
if self._on_event is not None:
|
|
400
|
+
self._on_event(event)
|
|
401
|
+
|
|
402
|
+
def _figsize_of(self):
|
|
403
|
+
if self.figsize is not None:
|
|
404
|
+
return self.figsize
|
|
405
|
+
return (4.8 * self.cols, 3.6 * self.rows)
|
|
406
|
+
|
|
407
|
+
def _ensure_inline(self):
|
|
408
|
+
if self._inline_fig is None:
|
|
409
|
+
fig, axes = plt.subplots(self.rows, self.cols, squeeze=False, figsize=self._figsize_of())
|
|
410
|
+
fig._cog_inline = True
|
|
411
|
+
self._inline_fig = fig
|
|
412
|
+
self._axes = [[axes[r][c] for c in range(self.cols)] for r in range(self.rows)]
|
|
413
|
+
self._bind_keys(fig)
|
|
414
|
+
return self._inline_fig
|
|
415
|
+
|
|
416
|
+
def _inline_axis(self, row, col, projection):
|
|
417
|
+
fig = self._ensure_inline()
|
|
418
|
+
ax = self._axes[row][col]
|
|
419
|
+
is3 = getattr(ax, "name", "") == "3d"
|
|
420
|
+
want3 = projection == "3d"
|
|
421
|
+
if is3 != want3:
|
|
422
|
+
ax.remove()
|
|
423
|
+
index = row * self.cols + col + 1
|
|
424
|
+
if want3:
|
|
425
|
+
ax = fig.add_subplot(self.rows, self.cols, index, projection="3d")
|
|
426
|
+
else:
|
|
427
|
+
ax = fig.add_subplot(self.rows, self.cols, index)
|
|
428
|
+
self._axes[row][col] = ax
|
|
429
|
+
return fig, ax
|
|
430
|
+
|
|
431
|
+
def _bind_keys(self, fig):
|
|
432
|
+
def on_key(event):
|
|
433
|
+
self._emit({"type": "key", "key": getattr(event, "key", None), "source": "mpl"})
|
|
434
|
+
|
|
435
|
+
try:
|
|
436
|
+
fig.canvas.mpl_connect("key_press_event", on_key)
|
|
437
|
+
except Exception:
|
|
438
|
+
pass
|
|
439
|
+
|
|
440
|
+
def _new_window(self, row, col, figbar, projection):
|
|
441
|
+
x, y, w, h = gridfig_box(row, col, self.rows, self.cols, self.mon)
|
|
442
|
+
prev = plt.rcParams.get("toolbar")
|
|
443
|
+
plt.rcParams["toolbar"] = "toolbar2" if figbar else "None"
|
|
444
|
+
try:
|
|
445
|
+
if projection:
|
|
446
|
+
fig, ax = plt.subplots(subplot_kw={"projection": projection})
|
|
447
|
+
else:
|
|
448
|
+
fig, ax = plt.subplots()
|
|
449
|
+
finally:
|
|
450
|
+
if prev is not None:
|
|
451
|
+
plt.rcParams["toolbar"] = prev
|
|
452
|
+
_place_window(fig, x, y, w, h)
|
|
453
|
+
self._bind_keys(fig)
|
|
454
|
+
try:
|
|
455
|
+
fig.tight_layout()
|
|
456
|
+
except Exception:
|
|
457
|
+
pass
|
|
458
|
+
return fig, ax
|
|
459
|
+
|
|
460
|
+
def _detach(self, cell):
|
|
461
|
+
if cell is None:
|
|
462
|
+
return
|
|
463
|
+
if cell.cv_name:
|
|
464
|
+
try:
|
|
465
|
+
import cv2
|
|
466
|
+
|
|
467
|
+
cv2.destroyWindow(cell.cv_name)
|
|
468
|
+
except Exception:
|
|
469
|
+
pass
|
|
470
|
+
cell.cv_name = None
|
|
471
|
+
if cell.cloud is not None:
|
|
472
|
+
cell.cloud._on_change = None
|
|
473
|
+
cell.cloud.close()
|
|
474
|
+
cell.cloud = None
|
|
475
|
+
if cell.owns_fig and cell.fig is not None:
|
|
476
|
+
plt.close(cell.fig)
|
|
477
|
+
cell.fig = None
|
|
478
|
+
cell.ax = None
|
|
479
|
+
|
|
480
|
+
def _forget_artists(self, cell):
|
|
481
|
+
cell.line = None
|
|
482
|
+
cell.image = None
|
|
483
|
+
cell.cbar = False
|
|
484
|
+
|
|
485
|
+
def _mpl(self, row, col, projection, figbar=False):
|
|
486
|
+
row, col = self._rc(row, col)
|
|
487
|
+
key = (row, col)
|
|
488
|
+
cell = self._cells.get(key)
|
|
489
|
+
if cell is not None and cell.kind == "cv":
|
|
490
|
+
self._detach(cell)
|
|
491
|
+
self._cells.pop(key, None)
|
|
492
|
+
cell = None
|
|
493
|
+
if cell is not None and cell.kind == "3d":
|
|
494
|
+
self._detach(cell)
|
|
495
|
+
self._cells.pop(key, None)
|
|
496
|
+
cell = None
|
|
497
|
+
if self._is_inline():
|
|
498
|
+
fig, ax = self._inline_axis(row, col, projection)
|
|
499
|
+
switched = cell is not None and cell.ax is not None and cell.ax is not ax
|
|
500
|
+
if cell is None:
|
|
501
|
+
cell = Cell("mpl")
|
|
502
|
+
self._cells[key] = cell
|
|
503
|
+
elif switched:
|
|
504
|
+
self._forget_artists(cell)
|
|
505
|
+
cell.kind = "mpl"
|
|
506
|
+
cell.fig = fig
|
|
507
|
+
cell.ax = ax
|
|
508
|
+
cell.owns_fig = False
|
|
509
|
+
return cell
|
|
510
|
+
|
|
511
|
+
want3 = projection == "3d"
|
|
512
|
+
if cell is not None and cell.kind == "mpl" and cell.ax is not None:
|
|
513
|
+
is3 = getattr(cell.ax, "name", "") == "3d"
|
|
514
|
+
if is3 == want3:
|
|
515
|
+
return cell
|
|
516
|
+
self._detach(cell)
|
|
517
|
+
self._cells.pop(key, None)
|
|
518
|
+
elif cell is not None:
|
|
519
|
+
self._detach(cell)
|
|
520
|
+
self._cells.pop(key, None)
|
|
521
|
+
fig, ax = self._new_window(row, col, figbar, projection)
|
|
522
|
+
cell = Cell("mpl")
|
|
523
|
+
cell.fig = fig
|
|
524
|
+
cell.ax = ax
|
|
525
|
+
cell.owns_fig = True
|
|
526
|
+
cell.figbar = bool(figbar)
|
|
527
|
+
self._cells[key] = cell
|
|
528
|
+
return cell
|
|
529
|
+
|
|
530
|
+
def _mpl2d(self, row, col, figbar=False):
|
|
531
|
+
return self._mpl(row, col, None, figbar=figbar)
|
|
532
|
+
|
|
533
|
+
def _mpl3d(self, row, col):
|
|
534
|
+
return self._mpl(row, col, "3d")
|
|
535
|
+
|
|
536
|
+
def _require_ax(self, row, col):
|
|
537
|
+
"""Axes for titles and limits, without flipping 2D/3D."""
|
|
538
|
+
row, col = self._rc(row, col)
|
|
539
|
+
cell = self._cells.get((row, col))
|
|
540
|
+
if cell is not None and cell.ax is not None:
|
|
541
|
+
return cell
|
|
542
|
+
if self._is_inline() and self._axes is not None:
|
|
543
|
+
ax = self._axes[row][col]
|
|
544
|
+
cell = Cell("mpl")
|
|
545
|
+
cell.fig = self._inline_fig
|
|
546
|
+
cell.ax = ax
|
|
547
|
+
self._cells[(row, col)] = cell
|
|
548
|
+
return cell
|
|
549
|
+
return self._mpl2d(row, col)
|
|
550
|
+
|
|
551
|
+
def _touch(self, fig=None):
|
|
552
|
+
fig = fig or self._inline_fig
|
|
553
|
+
if self._is_inline():
|
|
554
|
+
_display_inline(fig)
|
|
555
|
+
return
|
|
556
|
+
if self._pause:
|
|
557
|
+
_flush_fig(fig)
|
|
558
|
+
plt.pause(0.0001)
|
|
559
|
+
|
|
560
|
+
def _present(self):
|
|
561
|
+
if self._inline_fig is not None:
|
|
562
|
+
_display_inline(self._inline_fig)
|
|
563
|
+
for cell in self._cells.values():
|
|
564
|
+
if cell.owns_fig and cell.fig is not None:
|
|
565
|
+
_flush_fig(cell.fig)
|
|
566
|
+
|
|
567
|
+
def drawnow(self):
|
|
568
|
+
_drawnow()
|
|
569
|
+
|
|
570
|
+
def interactive(self, val):
|
|
571
|
+
if _as_on(val):
|
|
572
|
+
plt.ion()
|
|
573
|
+
else:
|
|
574
|
+
plt.ioff()
|
|
575
|
+
|
|
576
|
+
def pause(self, val=0.0001):
|
|
577
|
+
if self._is_inline():
|
|
578
|
+
self.drawnow()
|
|
579
|
+
return
|
|
580
|
+
plt.pause(val)
|
|
581
|
+
|
|
582
|
+
def reset(self, rows=None, cols=None, mon=None):
|
|
583
|
+
self.closeAll()
|
|
584
|
+
if rows is not None:
|
|
585
|
+
self.rows = max(1, int(rows))
|
|
586
|
+
if cols is not None:
|
|
587
|
+
self.cols = max(1, int(cols))
|
|
588
|
+
if mon is not None:
|
|
589
|
+
self.mon = int(mon)
|
|
590
|
+
|
|
591
|
+
def close(self, row, col):
|
|
592
|
+
key = self._rc(row, col)
|
|
593
|
+
cell = self._cells.pop(key, None)
|
|
594
|
+
if cell is not None:
|
|
595
|
+
self._detach(cell)
|
|
596
|
+
if self._is_inline() and self._axes is not None:
|
|
597
|
+
try:
|
|
598
|
+
self._axes[key[0]][key[1]].cla()
|
|
599
|
+
except Exception:
|
|
600
|
+
pass
|
|
601
|
+
|
|
602
|
+
def closeAll(self):
|
|
603
|
+
for key in list(self._cells):
|
|
604
|
+
self.close(*key)
|
|
605
|
+
if self._inline_fig is not None:
|
|
606
|
+
plt.close(self._inline_fig)
|
|
607
|
+
self._inline_fig = None
|
|
608
|
+
self._axes = None
|
|
609
|
+
|
|
610
|
+
def clf(self, row, col):
|
|
611
|
+
key = self._rc(row, col)
|
|
612
|
+
cell = self._cells.get(key)
|
|
613
|
+
if cell is None:
|
|
614
|
+
if self._is_inline() and self._axes is not None:
|
|
615
|
+
self._axes[key[0]][key[1]].cla()
|
|
616
|
+
return
|
|
617
|
+
cell.hold = False
|
|
618
|
+
self._forget_artists(cell)
|
|
619
|
+
if cell.kind == "3d" and cell.cloud is not None:
|
|
620
|
+
cell.cloud.clear()
|
|
621
|
+
return
|
|
622
|
+
if cell.ax is not None:
|
|
623
|
+
cell.ax.cla()
|
|
624
|
+
self._touch(cell.fig)
|
|
625
|
+
|
|
626
|
+
def cla(self, row, col):
|
|
627
|
+
self.clf(row, col)
|
|
628
|
+
|
|
629
|
+
def clfAll(self):
|
|
630
|
+
for key in list(self._cells):
|
|
631
|
+
self.clf(*key)
|
|
632
|
+
if self._is_inline() and self._axes is not None:
|
|
633
|
+
for row in self._axes:
|
|
634
|
+
for ax in row:
|
|
635
|
+
ax.cla()
|
|
636
|
+
|
|
637
|
+
def hold(self, row, col, state="on"):
|
|
638
|
+
cell = self._require_ax(row, col)
|
|
639
|
+
cell.hold = _as_on(state)
|
|
640
|
+
cell.ax._cog_hold = cell.hold
|
|
641
|
+
return cell.ax
|
|
642
|
+
|
|
643
|
+
def title(self, row, col, text):
|
|
644
|
+
row, col = self._rc(row, col)
|
|
645
|
+
cell = self._cells.get((row, col))
|
|
646
|
+
if cell is not None and cell.kind == "3d" and cell.cloud is not None:
|
|
647
|
+
cell.cloud.title = text
|
|
648
|
+
if self._is_inline() and self._axes is not None:
|
|
649
|
+
self._axes[row][col].set_title(text)
|
|
650
|
+
self._touch(self._inline_fig)
|
|
651
|
+
return
|
|
652
|
+
if cell is not None and cell.kind == "cv" and cell.cv_name:
|
|
653
|
+
import cv2
|
|
654
|
+
|
|
655
|
+
cv2.setWindowTitle(cell.cv_name, str(text))
|
|
656
|
+
return
|
|
657
|
+
cell = self._require_ax(row, col)
|
|
658
|
+
cell.ax.set_title(text)
|
|
659
|
+
self._touch(cell.fig)
|
|
660
|
+
return cell.ax
|
|
661
|
+
|
|
662
|
+
def xlabel(self, row, col, text):
|
|
663
|
+
cell = self._require_ax(row, col)
|
|
664
|
+
cell.ax.set_xlabel(text)
|
|
665
|
+
self._touch(cell.fig)
|
|
666
|
+
return cell.ax
|
|
667
|
+
|
|
668
|
+
def ylabel(self, row, col, text):
|
|
669
|
+
cell = self._require_ax(row, col)
|
|
670
|
+
cell.ax.set_ylabel(text)
|
|
671
|
+
self._touch(cell.fig)
|
|
672
|
+
return cell.ax
|
|
673
|
+
|
|
674
|
+
def axis(self, row, col, *args, **kwargs):
|
|
675
|
+
cell = self._require_ax(row, col)
|
|
676
|
+
result = cell.ax.axis(*args, **kwargs)
|
|
677
|
+
self._touch(cell.fig)
|
|
678
|
+
return result
|
|
679
|
+
|
|
680
|
+
def grid(self, row, col, on=True):
|
|
681
|
+
cell = self._require_ax(row, col)
|
|
682
|
+
if _as_on(on):
|
|
683
|
+
gridon(cell.ax)
|
|
684
|
+
else:
|
|
685
|
+
gridoff(cell.ax)
|
|
686
|
+
self._touch(cell.fig)
|
|
687
|
+
return cell.ax
|
|
688
|
+
|
|
689
|
+
def xlim(self, row, col, *args, **kwargs):
|
|
690
|
+
cell = self._require_ax(row, col)
|
|
691
|
+
result = cell.ax.set_xlim(*args, **kwargs)
|
|
692
|
+
self._touch(cell.fig)
|
|
693
|
+
return result
|
|
694
|
+
|
|
695
|
+
def ylim(self, row, col, *args, **kwargs):
|
|
696
|
+
cell = self._require_ax(row, col)
|
|
697
|
+
result = cell.ax.set_ylim(*args, **kwargs)
|
|
698
|
+
self._touch(cell.fig)
|
|
699
|
+
return result
|
|
700
|
+
|
|
701
|
+
def zlim(self, row, col, *args, **kwargs):
|
|
702
|
+
cell = self._mpl3d(row, col)
|
|
703
|
+
result = cell.ax.set_zlim(*args, **kwargs)
|
|
704
|
+
self._touch(cell.fig)
|
|
705
|
+
return result
|
|
706
|
+
|
|
707
|
+
def legend(self, row, col, *args, **kwargs):
|
|
708
|
+
cell = self._require_ax(row, col)
|
|
709
|
+
result = cell.ax.legend(*args, **kwargs)
|
|
710
|
+
self._touch(cell.fig)
|
|
711
|
+
return result
|
|
712
|
+
|
|
713
|
+
def colorbar(self, row, col, **kwargs):
|
|
714
|
+
cell = self._require_ax(row, col)
|
|
715
|
+
if cell.image is None:
|
|
716
|
+
return None
|
|
717
|
+
bar = cell.fig.colorbar(cell.image, ax=cell.ax, **kwargs)
|
|
718
|
+
cell.cbar = True
|
|
719
|
+
self._touch(cell.fig)
|
|
720
|
+
return bar
|
|
721
|
+
|
|
722
|
+
def text(self, row, col, txt, xy=(0, 0), **kwargs):
|
|
723
|
+
kwargs.pop("flush", None)
|
|
724
|
+
cell = self._mpl2d(row, col)
|
|
725
|
+
kwargs.setdefault("ha", "left")
|
|
726
|
+
kwargs.setdefault("va", "top")
|
|
727
|
+
artist = cell.ax.text(xy[0], xy[1], txt, **kwargs)
|
|
728
|
+
self._touch(cell.fig)
|
|
729
|
+
return artist
|
|
730
|
+
|
|
731
|
+
def rectangle(self, row, col, x, y, w, h, **kwargs):
|
|
732
|
+
cell = self._mpl2d(row, col)
|
|
733
|
+
kwargs.setdefault("fill", False)
|
|
734
|
+
patch = Rectangle((x, y), w, h, **kwargs)
|
|
735
|
+
cell.ax.add_patch(patch)
|
|
736
|
+
self._touch(cell.fig)
|
|
737
|
+
return patch
|
|
738
|
+
|
|
739
|
+
def box(self, row, col, x, y, hold=False, **kwargs):
|
|
740
|
+
kwargs.pop("flush", None)
|
|
741
|
+
if hold:
|
|
742
|
+
self.hold(row, col, "on")
|
|
743
|
+
xs = [x[0], x[1], x[1], x[0], x[0]]
|
|
744
|
+
ys = [y[0], y[0], y[1], y[1], y[0]]
|
|
745
|
+
fmt = kwargs.pop("fmt", "-")
|
|
746
|
+
return self.plot(row, col, xs, ys, fmt, grid=kwargs.pop("grid", 0), **kwargs)
|
|
747
|
+
|
|
748
|
+
def imshow(self, row, col, im, auto=0, title=None, figbar=False, viewer=None, cbar=False, **kwargs):
|
|
749
|
+
kwargs.pop("flush", None)
|
|
750
|
+
if str(viewer or "mpl").lower() in ("cv", "cv2", "opencv"):
|
|
751
|
+
prep = {key: kwargs.pop(key) for key in ("normalize", "colormap", "rgb") if key in kwargs}
|
|
752
|
+
bgr = prepare_bgr(im, **prep)
|
|
753
|
+
if bgr is None:
|
|
754
|
+
return None
|
|
755
|
+
if not self._is_inline():
|
|
756
|
+
return self._show_cv(row, col, bgr, title=title)
|
|
757
|
+
im = np.ascontiguousarray(bgr[:, :, ::-1])
|
|
758
|
+
cell = self._mpl2d(row, col, figbar=figbar)
|
|
759
|
+
ax = cell.ax
|
|
760
|
+
if cell.image is None or getattr(cell.image, "axes", None) is None:
|
|
761
|
+
cell.image = _show_image(ax, im, **kwargs)
|
|
762
|
+
else:
|
|
763
|
+
updateImage(cell.image, im, auto=bool(auto), flush=False)
|
|
764
|
+
if cbar and not cell.cbar and cell.image is not None:
|
|
765
|
+
ax.get_figure().colorbar(cell.image, ax=ax)
|
|
766
|
+
cell.cbar = True
|
|
767
|
+
if title is not None:
|
|
768
|
+
ax.set_title(title)
|
|
769
|
+
self._touch(cell.fig)
|
|
770
|
+
return ax
|
|
771
|
+
|
|
772
|
+
def imagesc(self, row, col, im, **kwargs):
|
|
773
|
+
kwargs.pop("flush", None)
|
|
774
|
+
data = np.asarray(im, dtype=float)
|
|
775
|
+
finite = data[np.isfinite(data)]
|
|
776
|
+
if finite.size == 0:
|
|
777
|
+
lo, hi = 0.0, 1.0
|
|
778
|
+
else:
|
|
779
|
+
lo, hi = float(finite.min()), float(finite.max())
|
|
780
|
+
kwargs.setdefault("vmin", lo)
|
|
781
|
+
kwargs.setdefault("vmax", hi)
|
|
782
|
+
kwargs.setdefault("cmap", "jet")
|
|
783
|
+
return self.imshow(row, col, im, **kwargs)
|
|
784
|
+
|
|
785
|
+
def _show_cv(self, row, col, bgr, title=None):
|
|
786
|
+
import cv2
|
|
787
|
+
|
|
788
|
+
row, col = self._rc(row, col)
|
|
789
|
+
key = (row, col)
|
|
790
|
+
cell = self._cells.get(key)
|
|
791
|
+
if cell is None or cell.kind != "cv":
|
|
792
|
+
if cell is not None:
|
|
793
|
+
self._detach(cell)
|
|
794
|
+
name = f"cog{id(self) % 100000}:{row},{col}"
|
|
795
|
+
cv2.namedWindow(name, cv2.WINDOW_NORMAL)
|
|
796
|
+
x, y, w, h = gridfig_box(row, col, self.rows, self.cols, self.mon)
|
|
797
|
+
from cogmatplot.place import place_cv_window
|
|
798
|
+
|
|
799
|
+
place_cv_window(name, x, y, w, h)
|
|
800
|
+
cell = Cell("cv")
|
|
801
|
+
cell.cv_name = name
|
|
802
|
+
self._cells[key] = cell
|
|
803
|
+
cv2.imshow(cell.cv_name, bgr)
|
|
804
|
+
if title:
|
|
805
|
+
cv2.setWindowTitle(cell.cv_name, str(title))
|
|
806
|
+
self._touch()
|
|
807
|
+
return None
|
|
808
|
+
|
|
809
|
+
def plot(self, row, col, x, y, fmt=".-", grid=1, ylabel=None, xlabel=None, figbar=False, hold=None, **kwargs):
|
|
810
|
+
kwargs.pop("flush", None)
|
|
811
|
+
cell = self._mpl2d(row, col, figbar=figbar)
|
|
812
|
+
ax = cell.ax
|
|
813
|
+
if hold is not None:
|
|
814
|
+
cell.hold = _as_on(hold)
|
|
815
|
+
if cell.line is not None and not cell.hold:
|
|
816
|
+
cell.line.set_data(x, y)
|
|
817
|
+
ax.relim()
|
|
818
|
+
ax.autoscale_view()
|
|
819
|
+
else:
|
|
820
|
+
(line,) = ax.plot(x, y, fmt, **kwargs)
|
|
821
|
+
if not cell.hold:
|
|
822
|
+
cell.line = line
|
|
823
|
+
if xlabel:
|
|
824
|
+
ax.set_xlabel(xlabel)
|
|
825
|
+
if ylabel:
|
|
826
|
+
ax.set_ylabel(ylabel)
|
|
827
|
+
if grid:
|
|
828
|
+
gridon(ax)
|
|
829
|
+
self._touch(cell.fig)
|
|
830
|
+
return ax
|
|
831
|
+
|
|
832
|
+
def plot3(self, row, col, x, y=None, z=None, fmt=".-", hold=None, color=None, colors=None, point_size=3, **kwargs):
|
|
833
|
+
"""MATLAB ``plot3(X, Y, Z)`` and ``plot3(X, Y, Z, LineSpec)``.
|
|
834
|
+
|
|
835
|
+
A line spec such as ``'-'`` or ``'.-'`` draws a 3D line. A marker spec
|
|
836
|
+
such as ``'.'``, or an ``(N, 3)`` point array, draws a point cloud.
|
|
837
|
+
``colors`` is per-point rgb.
|
|
838
|
+
"""
|
|
839
|
+
kwargs.pop("flush", None)
|
|
840
|
+
if isinstance(y, str) and z is None:
|
|
841
|
+
fmt = y
|
|
842
|
+
y = None
|
|
843
|
+
points = _plot3_points(x, y, z, fmt, colors)
|
|
844
|
+
if points is not None:
|
|
845
|
+
return self.scatter3(
|
|
846
|
+
row, col, points, color=color, colors=colors, point_size=point_size,
|
|
847
|
+
hold=True if hold is None else hold, **kwargs,
|
|
848
|
+
)
|
|
849
|
+
cell = self._mpl3d(row, col)
|
|
850
|
+
ax = cell.ax
|
|
851
|
+
if hold is not None:
|
|
852
|
+
cell.hold = _as_on(hold)
|
|
853
|
+
if cell.line is not None and not cell.hold and hasattr(cell.line, "set_data_3d"):
|
|
854
|
+
cell.line.set_data_3d(x, y, z)
|
|
855
|
+
else:
|
|
856
|
+
(line,) = ax.plot(x, y, z, fmt, **kwargs)
|
|
857
|
+
if not cell.hold:
|
|
858
|
+
cell.line = line
|
|
859
|
+
self._touch(cell.fig)
|
|
860
|
+
return ax
|
|
861
|
+
|
|
862
|
+
def figure3(self, row, col, title=None, **kwargs):
|
|
863
|
+
del kwargs
|
|
864
|
+
from cogmatplot.view3 import Fig3d
|
|
865
|
+
|
|
866
|
+
row, col = self._rc(row, col)
|
|
867
|
+
key = (row, col)
|
|
868
|
+
cell = self._cells.get(key)
|
|
869
|
+
if cell is not None and cell.kind == "3d" and cell.cloud is not None:
|
|
870
|
+
if title:
|
|
871
|
+
cell.cloud.title = title
|
|
872
|
+
return cell.cloud
|
|
873
|
+
if cell is not None:
|
|
874
|
+
self._detach(cell)
|
|
875
|
+
self._cells.pop(key, None)
|
|
876
|
+
cloud = Fig3d(
|
|
877
|
+
title=title or "Fig3d",
|
|
878
|
+
show=not self._is_inline(),
|
|
879
|
+
grid_pos=(row, col, self.rows, self.cols, self.mon),
|
|
880
|
+
off_screen=self._is_inline(),
|
|
881
|
+
on_change=self._on_cloud if self._is_inline() else None,
|
|
882
|
+
)
|
|
883
|
+
cloud.row = row
|
|
884
|
+
cloud.col = col
|
|
885
|
+
cell = Cell("3d")
|
|
886
|
+
cell.cloud = cloud
|
|
887
|
+
self._cells[key] = cell
|
|
888
|
+
return cloud
|
|
889
|
+
|
|
890
|
+
def scatter3(self, row, col, points, color=None, colors=None, point_size=3, cmap=None, hold=True, **kwargs):
|
|
891
|
+
cloud = self.figure3(row, col)
|
|
892
|
+
cloud.scatter3(points, color=color, colors=colors, point_size=point_size, cmap=cmap, hold=hold, **kwargs)
|
|
893
|
+
return cloud
|
|
894
|
+
|
|
895
|
+
def plot_mesh(self, row, col, points, faces, color=None, colors=None, style="surface", hold=True, **kwargs):
|
|
896
|
+
"""Draw a triangle mesh. ``style`` is ``surface``, ``wireframe``, or ``both``."""
|
|
897
|
+
cloud = self.figure3(row, col)
|
|
898
|
+
cloud.plot_mesh(points, faces, color=color, colors=colors, style=style, hold=hold, **kwargs)
|
|
899
|
+
return cloud
|
|
900
|
+
|
|
901
|
+
def view3(self, row, col, which="xy"):
|
|
902
|
+
self.figure3(row, col).view(which)
|
|
903
|
+
|
|
904
|
+
def grid3(self, row, col, on=True):
|
|
905
|
+
self.figure3(row, col).grid(on)
|
|
906
|
+
|
|
907
|
+
def clear3(self, row, col):
|
|
908
|
+
key = self._rc(row, col)
|
|
909
|
+
cell = self._cells.get(key)
|
|
910
|
+
if cell is not None and cell.cloud is not None:
|
|
911
|
+
cell.cloud.clear()
|
|
912
|
+
|
|
913
|
+
def _on_cloud(self, cloud):
|
|
914
|
+
if not self._is_inline():
|
|
915
|
+
self._touch()
|
|
916
|
+
return
|
|
917
|
+
row, col = cloud.row, cloud.col
|
|
918
|
+
fig = self._ensure_inline()
|
|
919
|
+
if not cloud.layers:
|
|
920
|
+
_fig, ax = self._inline_axis(row, col, None)
|
|
921
|
+
ax.cla()
|
|
922
|
+
if cloud.title:
|
|
923
|
+
ax.set_title(cloud.title)
|
|
924
|
+
self._touch(fig)
|
|
925
|
+
return
|
|
926
|
+
try:
|
|
927
|
+
image = cloud.snapshot()
|
|
928
|
+
_fig, ax = self._inline_axis(row, col, None)
|
|
929
|
+
ax.clear()
|
|
930
|
+
ax.imshow(image)
|
|
931
|
+
ax.axis("off")
|
|
932
|
+
except Exception as exc:
|
|
933
|
+
_fig, ax = self._inline_axis(row, col, "3d")
|
|
934
|
+
ax.cla()
|
|
935
|
+
for layer in cloud.layers:
|
|
936
|
+
_draw_mpl_layer(ax, layer)
|
|
937
|
+
if not self._cloud_warned:
|
|
938
|
+
self._cloud_warned = True
|
|
939
|
+
warnings.warn(
|
|
940
|
+
f"inline 3d render failed ({exc}); drew a matplotlib view instead",
|
|
941
|
+
RuntimeWarning,
|
|
942
|
+
stacklevel=2,
|
|
943
|
+
)
|
|
944
|
+
if cloud.title:
|
|
945
|
+
ax.set_title(cloud.title)
|
|
946
|
+
self._touch(fig)
|
|
947
|
+
|
|
948
|
+
|
|
949
|
+
__all__ = [
|
|
950
|
+
"FigureManager",
|
|
951
|
+
"saveFig",
|
|
952
|
+
"closeAll",
|
|
953
|
+
"clfAll",
|
|
954
|
+
"figure",
|
|
955
|
+
"gridfig",
|
|
956
|
+
"fit",
|
|
957
|
+
"axfit",
|
|
958
|
+
"drawnow",
|
|
959
|
+
"labels",
|
|
960
|
+
"imshow",
|
|
961
|
+
"imagesc",
|
|
962
|
+
"updateImage",
|
|
963
|
+
"colorbar",
|
|
964
|
+
"gridon",
|
|
965
|
+
"gridoff",
|
|
966
|
+
"hold",
|
|
967
|
+
]
|