1
+ from bisect import insort
1
2
from typing import Sequence
2
3
4
+ import numpy as np
5
+
3
6
from .containers import DataContainer , ArrayContainer , DataUnion
4
7
from .description import Desc , desc_like
5
- from .conversion_edge import Edge , TransformEdge
8
+ from .conversion_edge import Edge , Graph , TransformEdge
6
9
7
10
8
11
class Artist :
@@ -16,11 +19,28 @@ def __init__(
16
19
self ._container = DataUnion (container , kwargs_cont )
17
20
18
21
edges = edges or []
19
- self ._edges = list (edges )
20
-
21
- def draw (self , renderer , edges : Sequence [Edge ]) -> None :
22
+ self ._visible = True
23
+ self ._graph = Graph (edges )
24
+ self ._clip_box : DataContainer = ArrayContainer (
25
+ {"x" : "parent" , "y" : "parent" },
26
+ ** {"x" : np .asarray ([0 , 1 ]), "y" : np .asarray ([0 , 1 ])}
27
+ )
28
+
29
+ def draw (self , renderer , graph : Graph ) -> None :
22
30
return
23
31
32
+ def set_clip_box (self , container : DataContainer ) -> None :
33
+ self ._clip_box = container
34
+
35
+ def get_clip_box (self , container : DataContainer ) -> DataContainer :
36
+ return self ._clip_box
37
+
38
+ def get_visible (self ):
39
+ return self ._visible
40
+
41
+ def set_visible (self , visible ):
42
+ self ._visible = visible
43
+
24
44
25
45
class CompatibilityArtist :
26
46
"""A compatibility shim to ducktype as a classic Matplotlib Artist.
@@ -42,10 +62,44 @@ class CompatibilityArtist:
42
62
def __init__ (self , artist : Artist ):
43
63
self ._artist = artist
44
64
45
- self .axes = None
65
+ self ._axes = None
46
66
self .figure = None
47
67
self ._clippath = None
68
+ self ._visible = True
48
69
self .zorder = 2
70
+ self ._graph = Graph ([])
71
+
72
+ @property
73
+ def axes (self ):
74
+ return self ._axes
75
+
76
+ @axes .setter
77
+ def axes (self , ax ):
78
+ self ._axes = ax
79
+
80
+ if self ._axes is None :
81
+ self ._graph = Graph ([])
82
+ return
83
+
84
+ desc : Desc = Desc (("N" ,), coordinates = "data" )
85
+ xy : dict [str , Desc ] = {"x" : desc , "y" : desc }
86
+ self ._graph = Graph (
87
+ [
88
+ TransformEdge (
89
+ "data" ,
90
+ xy ,
91
+ desc_like (xy , coordinates = "axes" ),
92
+ transform = self ._axes .transData - self ._axes .transAxes ,
93
+ ),
94
+ TransformEdge (
95
+ "axes" ,
96
+ desc_like (xy , coordinates = "axes" ),
97
+ desc_like (xy , coordinates = "display" ),
98
+ transform = self ._axes .transAxes ,
99
+ ),
100
+ ],
101
+ aliases = (("parent" , "axes" ),),
102
+ )
49
103
50
104
def set_figure (self , fig ):
51
105
self .figure = fig
@@ -65,32 +119,19 @@ def set_clip_path(self, path):
65
119
def get_animated (self ):
66
120
return False
67
121
68
- def draw (self , renderer , edges = None ):
122
+ def get_visible (self ):
123
+ return self ._visible
69
124
70
- if edges is None :
71
- edges = []
125
+ def set_visible ( self , visible ) :
126
+ self . _visible = visible
72
127
73
- if self .axes is not None :
74
- desc : Desc = Desc (("N" ,), coordinates = "data" )
75
- xy : dict [str , Desc ] = {"x" : desc , "y" : desc }
76
- edges .append (
77
- TransformEdge (
78
- "data" ,
79
- xy ,
80
- desc_like (xy , coordinates = "axes" ),
81
- transform = self .axes .transData - self .axes .transAxes ,
82
- )
83
- )
84
- edges .append (
85
- TransformEdge (
86
- "axes" ,
87
- desc_like (xy , coordinates = "axes" ),
88
- desc_like (xy , coordinates = "display" ),
89
- transform = self .axes .transAxes ,
90
- )
91
- )
128
+ def draw (self , renderer , graph = None ):
129
+ if not self .get_visible ():
130
+ return
92
131
93
- self ._artist .draw (renderer , edges )
132
+ if graph is None :
133
+ graph = Graph ([])
134
+ self ._artist .draw (renderer , graph + self ._graph )
94
135
95
136
96
137
class CompatibilityAxes :
@@ -111,11 +152,44 @@ class CompatibilityAxes:
111
152
"""
112
153
113
154
def __init__ (self , axes ):
114
- self .axes = axes
155
+ self ._axes = axes
115
156
self .figure = None
116
157
self ._clippath = None
158
+ self ._visible = True
117
159
self .zorder = 2
118
- self ._children = []
160
+ self ._children : list [tuple [float , Artist ]] = []
161
+
162
+ @property
163
+ def axes (self ):
164
+ return self ._axes
165
+
166
+ @axes .setter
167
+ def axes (self , ax ):
168
+ self ._axes = ax
169
+
170
+ if self ._axes is None :
171
+ self ._graph = Graph ([])
172
+ return
173
+
174
+ desc : Desc = Desc (("N" ,), coordinates = "data" )
175
+ xy : dict [str , Desc ] = {"x" : desc , "y" : desc }
176
+ self ._graph = Graph (
177
+ [
178
+ TransformEdge (
179
+ "data" ,
180
+ xy ,
181
+ desc_like (xy , coordinates = "axes" ),
182
+ transform = self ._axes .transData - self ._axes .transAxes ,
183
+ ),
184
+ TransformEdge (
185
+ "axes" ,
186
+ desc_like (xy , coordinates = "axes" ),
187
+ desc_like (xy , coordinates = "display" ),
188
+ transform = self ._axes .transAxes ,
189
+ ),
190
+ ],
191
+ aliases = (("parent" , "axes" ),),
192
+ )
119
193
120
194
def set_figure (self , fig ):
121
195
self .figure = fig
@@ -135,39 +209,28 @@ def set_clip_path(self, path):
135
209
def get_animated (self ):
136
210
return False
137
211
138
- def draw (self , renderer , edges = None ):
139
- if edges is None :
140
- edges = []
212
+ def draw (self , renderer , graph = None ):
213
+ if not self .visible :
214
+ return
215
+ if graph is None :
216
+ graph = Graph ([])
141
217
142
- if self .axes is not None :
143
- desc : Desc = Desc (("N" ,), coordinates = "data" )
144
- xy : dict [str , Desc ] = {"x" : desc , "y" : desc }
145
- edges .append (
146
- TransformEdge (
147
- "data" ,
148
- xy ,
149
- desc_like (xy , coordinates = "axes" ),
150
- transform = self .axes .transData - self .axes .transAxes ,
151
- )
152
- )
153
- edges .append (
154
- TransformEdge (
155
- "axes" ,
156
- desc_like (xy , coordinates = "axes" ),
157
- desc_like (xy , coordinates = "display" ),
158
- transform = self .axes .transAxes ,
159
- )
160
- )
218
+ graph = graph + self ._graph
161
219
162
- # TODO independent zorder
163
- for c in self ._children :
164
- c .draw (renderer , edges )
220
+ for _ , c in self ._children :
221
+ c .draw (renderer , graph )
165
222
166
- def add_artist (self , artist ):
167
- self ._children . append ( artist )
223
+ def add_artist (self , artist , zorder = 1 ):
224
+ insort ( self ._children , ( zorder , artist ), key = lambda x : x [ 0 ] )
168
225
169
226
def set_xlim (self , min_ = None , max_ = None ):
170
227
self .axes .set_xlim (min_ , max_ )
171
228
172
229
def set_ylim (self , min_ = None , max_ = None ):
173
230
self .axes .set_ylim (min_ , max_ )
231
+
232
+ def get_visible (self ):
233
+ return self ._visible
234
+
235
+ def set_visible (self , visible ):
236
+ self ._visible = visible
0 commit comments