1+ from bisect import insort
12from typing import Sequence
23
4+ import numpy as np
5+
36from .containers import DataContainer , ArrayContainer , DataUnion
47from .description import Desc , desc_like
5- from .conversion_edge import Edge , TransformEdge
8+ from .conversion_edge import Edge , Graph , TransformEdge
69
710
811class Artist :
@@ -16,11 +19,28 @@ def __init__(
1619 self ._container = DataUnion (container , kwargs_cont )
1720
1821 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 :
2230 return
2331
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+
2444
2545class CompatibilityArtist :
2646 """A compatibility shim to ducktype as a classic Matplotlib Artist.
@@ -42,10 +62,44 @@ class CompatibilityArtist:
4262 def __init__ (self , artist : Artist ):
4363 self ._artist = artist
4464
45- self .axes = None
65+ self ._axes = None
4666 self .figure = None
4767 self ._clippath = None
68+ self ._visible = True
4869 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+ )
49103
50104 def set_figure (self , fig ):
51105 self .figure = fig
@@ -65,32 +119,19 @@ def set_clip_path(self, path):
65119 def get_animated (self ):
66120 return False
67121
68- def draw (self , renderer , edges = None ):
122+ def get_visible (self ):
123+ return self ._visible
69124
70- if edges is None :
71- edges = []
125+ def set_visible ( self , visible ) :
126+ self . _visible = visible
72127
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
92131
93- self ._artist .draw (renderer , edges )
132+ if graph is None :
133+ graph = Graph ([])
134+ self ._artist .draw (renderer , graph + self ._graph )
94135
95136
96137class CompatibilityAxes :
@@ -111,11 +152,44 @@ class CompatibilityAxes:
111152 """
112153
113154 def __init__ (self , axes ):
114- self .axes = axes
155+ self ._axes = axes
115156 self .figure = None
116157 self ._clippath = None
158+ self ._visible = True
117159 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+ )
119193
120194 def set_figure (self , fig ):
121195 self .figure = fig
@@ -135,39 +209,28 @@ def set_clip_path(self, path):
135209 def get_animated (self ):
136210 return False
137211
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 ([])
141217
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
161219
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 )
165222
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 ] )
168225
169226 def set_xlim (self , min_ = None , max_ = None ):
170227 self .axes .set_xlim (min_ , max_ )
171228
172229 def set_ylim (self , min_ = None , max_ = None ):
173230 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