22
22
import numpy as np
23
23
from matplotlib import cbook , mlab
24
24
from matplotlib .axes import Axes
25
+ from matplotlib .figure import Figure
25
26
26
27
27
28
# This is the magic line that must exist in pyplot, after which the boilerplate
34
35
35
36
# Autogenerated by boilerplate.py. Do not edit as changes will be lost."""
36
37
37
- CMAPPABLE_TEMPLATE = AUTOGEN_MSG + """
38
+ AXES_CMAPPABLE_METHOD_TEMPLATE = AUTOGEN_MSG + """
38
39
@docstring.copy(Axes.{called_name})
39
40
def {name}{signature}:
40
41
__ret = gca().{called_name}{call}
41
42
{sci_command}
42
43
return __ret
43
44
"""
44
45
45
- NON_CMAPPABLE_TEMPLATE = AUTOGEN_MSG + """
46
+ AXES_METHOD_TEMPLATE = AUTOGEN_MSG + """
46
47
@docstring.copy(Axes.{called_name})
47
48
def {name}{signature}:
48
49
return gca().{called_name}{call}
49
50
"""
50
51
52
+ FIGURE_METHOD_TEMPLATE = AUTOGEN_MSG + """
53
+ @docstring.copy(Figure.{called_name})
54
+ def {name}{signature}:
55
+ return gcf().{called_name}{call}
56
+ """
57
+
51
58
# Used for colormap functions
52
59
CMAP_TEMPLATE = AUTOGEN_MSG + '''
53
60
def {name}():
@@ -84,16 +91,16 @@ def __repr__(self):
84
91
return self ._repr
85
92
86
93
87
- def generate_function (name , called_name , template , ** kwargs ):
94
+ def generate_function (name , called_fullname , template , ** kwargs ):
88
95
"""
89
96
Create a wrapper function *pyplot_name* calling *call_name*.
90
97
91
98
Parameters
92
99
----------
93
100
name : str
94
101
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"`` .
97
104
template : str
98
105
The template to be used. The template must contain {}-style format
99
106
placeholders. The following placeholders are filled in:
@@ -111,7 +118,10 @@ def generate_function(name, called_name, template, **kwargs):
111
118
initial_indent = ' ' * 8 , subsequent_indent = ' ' * 8 )
112
119
113
120
# 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 ))
115
125
# Replace self argument.
116
126
params = list (signature .parameters .values ())[1 :]
117
127
signature = str (signature .replace (parameters = [
@@ -148,10 +158,10 @@ def generate_function(name, called_name, template, **kwargs):
148
158
if MAX_CALL_PREFIX + max (len (name ), len (called_name )) + len (call ) >= 80 :
149
159
call = '(\n ' + text_wrapper .fill (call [1 :])
150
160
# Bail out in case of name collision.
151
- for reserved in ('gca' , 'gci' , '__ret' ):
161
+ for reserved in ('gca' , 'gci' , 'gcf' , ' __ret' ):
152
162
if reserved in params :
153
163
raise ValueError (
154
- f'Axes method { called_name } has kwarg named { reserved } ' )
164
+ f'Method { called_fullname } has kwarg named { reserved } ' )
155
165
156
166
return template .format (
157
167
name = name ,
@@ -164,6 +174,14 @@ def generate_function(name, called_name, template, **kwargs):
164
174
def boilerplate_gen ():
165
175
"""Generator of lines for the automated part of pyplot."""
166
176
177
+ _figure_commands = (
178
+ 'figimage' ,
179
+ 'figtext:text' ,
180
+ 'ginput' ,
181
+ 'suptitle' ,
182
+ 'waitforbuttonpress' ,
183
+ )
184
+
167
185
# These methods are all simple wrappers of Axes methods by the same name.
168
186
_axes_commands = (
169
187
'acorr' ,
@@ -261,15 +279,23 @@ def boilerplate_gen():
261
279
'tripcolor' : 'sci(__ret)' ,
262
280
}
263
281
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
+
264
290
for spec in _axes_commands :
265
291
if ':' in spec :
266
292
name , called_name = spec .split (':' )
267
293
else :
268
294
name = called_name = spec
269
295
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 ,
273
299
sci_command = cmappable .get (name ))
274
300
275
301
cmaps = (
0 commit comments