@@ -508,6 +508,46 @@ def set(self, **kwargs):
508508 ret .extend ( [func (v )] )
509509 return ret
510510
511+ def findobj (self , match = None ):
512+ """
513+ recursively find all :class:matplotlib.artist.Artist instances
514+ contained in self
515+
516+ *match* can be
517+
518+ - None: return all objects contained in artist (including artist)
519+
520+ - function with signature ``boolean = match(artist)`` used to filter matches
521+
522+ - class instance: eg Line2D. Only return artists of class type
523+ """
524+
525+ if match is None : # always return True
526+ def matchfunc (x ): return True
527+ elif cbook .issubclass_safe (match , Artist ):
528+ def matchfunc (x ):
529+ return isinstance (x , match )
530+ elif callable (match ):
531+ matchfunc = match
532+ else :
533+ raise ValueError ('match must be None, an matplotlib.artist.Artist subclass, or a callable' )
534+
535+
536+ artists = []
537+ if hasattr (self , 'get_children' ):
538+ for c in self .get_children ():
539+ if matchfunc (c ):
540+ artists .append (c )
541+ artists .extend ([thisc for thisc in c .findobj (matchfunc ) if matchfunc (thisc )])
542+ else :
543+ if matchfunc (self ):
544+ artists .append (self )
545+ return artists
546+
547+
548+
549+
550+
511551
512552class ArtistInspector :
513553 """
@@ -690,6 +730,48 @@ def pprint_getters(self):
690730 return lines
691731
692732
733+
734+ def findobj (self , match = None ):
735+ """
736+ recursively find all :class:matplotlib.artist.Artist instances
737+ contained in self
738+
739+ if *match* is not None, it can be
740+
741+ - function with signature ``boolean = match(artist)``
742+
743+ - class instance: eg Line2D
744+
745+ used to filter matches
746+ """
747+
748+ if match is None : # always return True
749+ def matchfunc (x ): return True
750+ elif issubclass (match , Artist ):
751+ def matchfunc (x ):
752+ return isinstance (x , match )
753+ elif callable (match ):
754+ matchfunc = func
755+ else :
756+ raise ValueError ('match must be None, an matplotlib.artist.Artist subclass, or a callable' )
757+
758+
759+ artists = []
760+ if hasattr (self , 'get_children' ):
761+ for c in self .get_children ():
762+ if matchfunc (c ):
763+ artists .append (c )
764+ artists .extend ([thisc for thisc in c .findobj (matchfunc ) if matchfunc (thisc )])
765+ else :
766+ if matchfunc (self ):
767+ artists .append (self )
768+ return artists
769+
770+
771+
772+
773+
774+
693775def getp (o , property = None ):
694776 """
695777 Return the value of handle property. property is an optional string
0 commit comments