- Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathartist.py
371 lines (292 loc) · 10.9 KB
/
artist.py
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
frombisectimportinsort
fromcollectionsimportOrderedDict
fromtypingimportSequence
fromcontextlibimportcontextmanager
importnumpyasnp
frommatplotlib.backend_basesimportPickEvent
importmatplotlib.artistasmartist
from .containersimportDataContainer, ArrayContainer, DataUnion
from .descriptionimportDesc, desc_like
from .conversion_edgeimportEdge, FuncEdge, Graph, TransformEdge
classArtist:
required_keys: dict[str, Desc]
# defaults?
def__init__(
self, container: DataContainer, edges: Sequence[Edge] |None=None, **kwargs
):
kwargs_cont=ArrayContainer(**kwargs)
self._container=DataUnion(container, kwargs_cont)
self._children: list[tuple[float, Artist]] = []
self._picker=None
edges=edgesor []
self._visible=True
self._graph=Graph(edges)
self._clip_box: DataContainer=ArrayContainer(
{"x": "parent", "y": "parent"},
**{"x": np.asarray([0, 1]), "y": np.asarray([0, 1])}
)
self._caches= {}
defdraw(self, renderer, graph: Graph) ->None:
return
defset_clip_box(self, container: DataContainer) ->None:
self._clip_box=container
defget_clip_box(self, container: DataContainer) ->DataContainer:
returnself._clip_box
defget_visible(self):
returnself._visible
defset_visible(self, visible):
self._visible=visible
defpickable(self) ->bool:
returnself._pickerisnotNone
defget_picker(self):
returnself._picker
defset_picker(self, picker):
self._picker=picker
defcontains(self, mouseevent, graph=None):
"""
Test whether the artist contains the mouse event.
Parameters
----------
mouseevent : `~matplotlib.backend_bases.MouseEvent`
Returns
-------
contains : bool
Whether any values are within the radius.
details : dict
An artist-specific dictionary of details of the event context,
such as which points are contained in the pick radius. See the
individual Artist subclasses for details.
"""
returnFalse, {}
defget_children(self):
return [a[1] forainself._children]
defpick(self, mouseevent, graph: Graph|None=None):
"""
Process a pick event.
Each child artist will fire a pick event if *mouseevent* is over
the artist and the artist has picker set.
See Also
--------
set_picker, get_picker, pickable
"""
ifgraphisNone:
graph=self._graph
else:
graph=graph+self._graph
# Pick self
ifself.pickable():
picker=self.get_picker()
ifcallable(picker):
inside, prop=picker(self, mouseevent)
else:
inside, prop=self.contains(mouseevent, graph)
ifinside:
PickEvent(
"pick_event", mouseevent.canvas, mouseevent, self, **prop
)._process()
# Pick children
forainself.get_children():
# make sure the event happened in the same Axes
ax=getattr(a, "axes", None)
ifmouseevent.inaxesisNoneoraxisNoneormouseevent.inaxes==ax:
# we need to check if mouseevent.inaxes is None
# because some objects associated with an Axes (e.g., a
# tick label) can be outside the bounding box of the
# Axes and inaxes will be None
# also check that ax is None so that it traverse objects
# which do not have an axes property but children might
a.pick(mouseevent, graph)
def_get_dynamic_graph(self, query, description, graph, cacheset):
returnGraph([])
def_query_and_eval(self, container, requires, graph, cacheset=None):
g=graph+self._graph
query, q_cache_key=container.query(g)
g=g+self._get_dynamic_graph(query, container.describe(), graph, cacheset)
g_cache_key=g.cache_key()
cache_key= (g_cache_key, q_cache_key)
cache=None
ifcachesetisnotNone:
cache=self._caches.setdefault(cacheset, OrderedDict())
ifcache_keyincache:
returncache[cache_key]
conv=g.evaluator(container.describe(), requires)
ret=conv.evaluate(query)
# TODO: actually add to cache and prune
# if cache is not None:
# cache[cache_key] = ret
returnret
classCompatibilityArtist:
"""A compatibility shim to ducktype as a classic Matplotlib Artist.
At this time features are implemented on an "as needed" basis, and many
are only implemented insofar as they do not fail, not necessarily providing
full functionality of a full MPL Artist.
The idea is to keep the new Artist class as minimal as possible.
As features are added this may shrink.
The main thing we are trying to avoid is the reliance on the axes/figure
Ultimately for useability, whatever remains shimmed out here may be rolled in as
some form of gaurded option to ``Artist`` itself, but a firm dividing line is
useful for avoiding accidental dependency.
"""
def__init__(self, artist: martist.Artist):
self._artist=artist
self._axes=None
self.figure=None
self._clippath=None
self._visible=True
self.zorder=2
self._graph=Graph([])
@property
defaxes(self):
returnself._axes
@axes.setter
defaxes(self, ax):
self._axes=ax
ifself._axesisNone:
self._graph=Graph([])
return
desc: Desc=Desc(("N",), coordinates="data")
xy: dict[str, Desc] = {"x": desc, "y": desc}
self._graph=Graph(
[
TransformEdge(
"data",
xy,
desc_like(xy, coordinates="axes"),
transform=self._axes.transData-self._axes.transAxes,
),
TransformEdge(
"axes",
desc_like(xy, coordinates="axes"),
desc_like(xy, coordinates="display"),
transform=self._axes.transAxes,
),
],
aliases=(("parent", "axes"),),
)
defset_figure(self, fig):
self.figure=fig
defis_transform_set(self):
returnTrue
defget_mouseover(self):
returnFalse
defget_clip_path(self):
self._clippath
defset_clip_path(self, path):
self._clippath=path
defget_animated(self):
returnFalse
defget_visible(self):
returnself._visible
defset_visible(self, visible):
self._visible=visible
defdraw(self, renderer, graph=None):
ifnotself.get_visible():
return
ifgraphisNone:
graph=Graph([])
self._artist.draw(renderer, graph+self._graph)
classCompatibilityAxes(Artist):
"""A compatibility shim to add to traditional matplotlib axes.
At this time features are implemented on an "as needed" basis, and many
are only implemented insofar as they do not fail, not necessarily providing
full functionality of a full MPL Artist.
The idea is to keep the new Artist class as minimal as possible.
As features are added this may shrink.
The main thing we are trying to avoid is the reliance on the axes/figure
Ultimately for useability, whatever remains shimmed out here may be rolled in as
some form of gaurded option to ``Artist`` itself, but a firm dividing line is
useful for avoiding accidental dependency.
"""
def__init__(self, axes):
super().__init__(ArrayContainer())
self._axes=axes
self.figure=None
self._clippath=None
self.zorder=2
@property
defaxes(self):
returnself._axes
@axes.setter
defaxes(self, ax):
self._axes=ax
ifself._axesisNone:
self._graph=Graph([])
return
desc: Desc=Desc(("N",), coordinates="data")
desc_scal: Desc=Desc((), coordinates="data")
xy: dict[str, Desc] = {"x": desc, "y": desc}
xy_scal: dict[str, Desc] = {"x": desc_scal, "y": desc_scal}
self._graph=Graph(
[
TransformEdge(
"data",
xy,
desc_like(xy, coordinates="axes"),
transform=self._axes.transData-self._axes.transAxes,
),
TransformEdge(
"axes",
desc_like(xy, coordinates="axes"),
desc_like(xy, coordinates="display"),
transform=self._axes.transAxes,
),
TransformEdge(
"data_scal",
xy_scal,
desc_like(xy_scal, coordinates="axes"),
transform=self._axes.transData-self._axes.transAxes,
),
TransformEdge(
"axes_scal",
desc_like(xy_scal, coordinates="axes"),
desc_like(xy_scal, coordinates="display"),
transform=self._axes.transAxes,
),
FuncEdge.from_func(
"xunits",
lambda: self._axes.xaxis.units,
{},
{"xunits": Desc((), "units")},
),
FuncEdge.from_func(
"yunits",
lambda: self._axes.yaxis.units,
{},
{"yunits": Desc((), "units")},
),
],
aliases=(("parent", "axes"),),
)
defset_figure(self, fig):
self.figure=fig
defis_transform_set(self):
returnTrue
defget_mouseover(self):
returnFalse
defget_clip_path(self):
self._clippath
defset_clip_path(self, path):
self._clippath=path
defget_animated(self):
returnFalse
defdraw(self, renderer, graph=None):
ifnotself.get_visible():
return
ifgraphisNone:
graph=Graph([])
graph=graph+self._graph
for_, cinself._children:
c.draw(renderer, graph)
defadd_artist(self, artist, zorder=1):
insort(self._children, (zorder, artist), key=lambdax: x[0])
defset_xlim(self, min_=None, max_=None):
self.axes.set_xlim(min_, max_)
defset_ylim(self, min_=None, max_=None):
self.axes.set_ylim(min_, max_)
@contextmanager
def_renderer_group(renderer, group, gid):
renderer.open_group(group, gid)
try:
yield
finally:
renderer.close_group(group)