2222import numpy as np
2323from matplotlib import cbook , mlab
2424from matplotlib .axes import Axes
25+ from matplotlib .figure import Figure
2526
2627
2728# This is the magic line that must exist in pyplot, after which the boilerplate
3435
3536# Autogenerated by boilerplate.py. Do not edit as changes will be lost."""
3637
37- CMAPPABLE_TEMPLATE = AUTOGEN_MSG + """
38+ AXES_CMAPPABLE_METHOD_TEMPLATE = AUTOGEN_MSG + """
3839@docstring.copy(Axes.{called_name})
3940def {name}{signature}:
4041 __ret = gca().{called_name}{call}
4142 {sci_command}
4243 return __ret
4344"""
4445
45- NON_CMAPPABLE_TEMPLATE = AUTOGEN_MSG + """
46+ AXES_METHOD_TEMPLATE = AUTOGEN_MSG + """
4647@docstring.copy(Axes.{called_name})
4748def {name}{signature}:
4849 return gca().{called_name}{call}
4950"""
5051
52+ FIGURE_METHOD_TEMPLATE = AUTOGEN_MSG + """
53+ @docstring.copy(Figure.{called_name})
54+ def {name}{signature}:
55+ return gcf().{called_name}{call}
56+ """
57+
5158# Used for colormap functions
5259CMAP_TEMPLATE = AUTOGEN_MSG + '''
5360def {name}():
@@ -84,16 +91,16 @@ def __repr__(self):
8491 return self ._repr
8592
8693
87- def generate_function (name , called_name , template , ** kwargs ):
94+ def generate_function (name , called_fullname , template , ** kwargs ):
8895 """
8996 Create a wrapper function *pyplot_name* calling *call_name*.
9097
9198 Parameters
9299 ----------
93100 name : str
94101 The function to be created.
95- called_name : str
96- The function to be wrapped.
102+ called_fullname : str
103+ The method to be wrapped in the format ``"Class.method"`` .
97104 template : str
98105 The template to be used. The template must contain {}-style format
99106 placeholders. The following placeholders are filled in:
@@ -111,7 +118,10 @@ def generate_function(name, called_name, template, **kwargs):
111118 initial_indent = ' ' * 8 , subsequent_indent = ' ' * 8 )
112119
113120 # Get signature of wrapped function.
114- signature = inspect .signature (getattr (Axes , called_name ))
121+ class_name , called_name = called_fullname .split ('.' )
122+ class_ = {'Axes' : Axes , 'Figure' : Figure }[class_name ]
123+
124+ signature = inspect .signature (getattr (class_ , called_name ))
115125 # Replace self argument.
116126 params = list (signature .parameters .values ())[1 :]
117127 signature = str (signature .replace (parameters = [
@@ -148,10 +158,10 @@ def generate_function(name, called_name, template, **kwargs):
148158 if MAX_CALL_PREFIX + max (len (name ), len (called_name )) + len (call ) >= 80 :
149159 call = '(\n ' + text_wrapper .fill (call [1 :])
150160 # Bail out in case of name collision.
151- for reserved in ('gca' , 'gci' , '__ret' ):
161+ for reserved in ('gca' , 'gci' , 'gcf' , ' __ret' ):
152162 if reserved in params :
153163 raise ValueError (
154- f'Axes method { called_name } has kwarg named { reserved } ' )
164+ f'Method { called_fullname } has kwarg named { reserved } ' )
155165
156166 return template .format (
157167 name = name ,
@@ -164,6 +174,14 @@ def generate_function(name, called_name, template, **kwargs):
164174def boilerplate_gen ():
165175 """Generator of lines for the automated part of pyplot."""
166176
177+ _figure_commands = (
178+ 'figimage' ,
179+ 'figtext:text' ,
180+ 'ginput' ,
181+ 'suptitle' ,
182+ 'waitforbuttonpress' ,
183+ )
184+
167185 # These methods are all simple wrappers of Axes methods by the same name.
168186 _axes_commands = (
169187 'acorr' ,
@@ -261,15 +279,23 @@ def boilerplate_gen():
261279 'tripcolor' : 'sci(__ret)' ,
262280 }
263281
282+ for spec in _figure_commands :
283+ if ':' in spec :
284+ name , called_name = spec .split (':' )
285+ else :
286+ name = called_name = spec
287+ yield generate_function (name , f'Figure.{ called_name } ' ,
288+ FIGURE_METHOD_TEMPLATE )
289+
264290 for spec in _axes_commands :
265291 if ':' in spec :
266292 name , called_name = spec .split (':' )
267293 else :
268294 name = called_name = spec
269295
270- template = (CMAPPABLE_TEMPLATE if name in cmappable else
271- NON_CMAPPABLE_TEMPLATE )
272- yield generate_function (name , called_name , template ,
296+ template = (AXES_CMAPPABLE_METHOD_TEMPLATE if name in cmappable else
297+ AXES_METHOD_TEMPLATE )
298+ yield generate_function (name , f'Axes. { called_name } ' , template ,
273299 sci_command = cmappable .get (name ))
274300
275301 cmaps = (
0 commit comments