Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 48c7258

Browse filesBrowse files
authored
Merge pull request matplotlib#13811 from timhoffm/boilerplate-figure
Generate Figure method wrappers via boilerplate.py
2 parents 019aeea + 29f1c87 commit 48c7258
Copy full SHA for 48c7258

File tree

Expand file treeCollapse file tree

2 files changed

+79
-36
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+79
-36
lines changed

‎lib/matplotlib/pyplot.py

Copy file name to clipboardExpand all lines: lib/matplotlib/pyplot.py
+42-25Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -700,34 +700,9 @@ def savefig(*args, **kwargs):
700700
return res
701701

702702

703-
@docstring.copy(Figure.ginput)
704-
def ginput(*args, **kwargs):
705-
return gcf().ginput(*args, **kwargs)
706-
707-
708-
@docstring.copy(Figure.waitforbuttonpress)
709-
def waitforbuttonpress(*args, **kwargs):
710-
return gcf().waitforbuttonpress(*args, **kwargs)
711-
712-
713703
## Putting things in figures ##
714704

715705

716-
@docstring.copy(Figure.text)
717-
def figtext(x, y, s, *args, **kwargs):
718-
return gcf().text(x, y, s, *args, **kwargs)
719-
720-
721-
@docstring.copy(Figure.suptitle)
722-
def suptitle(t, **kwargs):
723-
return gcf().suptitle(t, **kwargs)
724-
725-
726-
@docstring.copy(Figure.figimage)
727-
def figimage(*args, **kwargs):
728-
return gcf().figimage(*args, **kwargs)
729-
730-
731706
def figlegend(*args, **kwargs):
732707
return gcf().legend(*args, **kwargs)
733708
if Figure.legend.__doc__:
@@ -2327,6 +2302,48 @@ def getname_val(identifier):
23272302
################# REMAINING CONTENT GENERATED BY boilerplate.py ##############
23282303

23292304

2305+
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
2306+
@docstring.copy(Figure.figimage)
2307+
def figimage(
2308+
X, xo=0, yo=0, alpha=None, norm=None, cmap=None, vmin=None,
2309+
vmax=None, origin=None, resize=False, **kwargs):
2310+
return gcf().figimage(
2311+
X, xo=xo, yo=yo, alpha=alpha, norm=norm, cmap=cmap, vmin=vmin,
2312+
vmax=vmax, origin=origin, resize=resize, **kwargs)
2313+
2314+
2315+
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
2316+
@docstring.copy(Figure.text)
2317+
def figtext(
2318+
x, y, s, fontdict=None,
2319+
withdash=cbook.deprecation._deprecated_parameter, **kwargs):
2320+
return gcf().text(
2321+
x, y, s, fontdict=fontdict, withdash=withdash, **kwargs)
2322+
2323+
2324+
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
2325+
@docstring.copy(Figure.ginput)
2326+
def ginput(
2327+
n=1, timeout=30, show_clicks=True, mouse_add=1, mouse_pop=3,
2328+
mouse_stop=2):
2329+
return gcf().ginput(
2330+
n=n, timeout=timeout, show_clicks=show_clicks,
2331+
mouse_add=mouse_add, mouse_pop=mouse_pop,
2332+
mouse_stop=mouse_stop)
2333+
2334+
2335+
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
2336+
@docstring.copy(Figure.suptitle)
2337+
def suptitle(t, **kwargs):
2338+
return gcf().suptitle(t, **kwargs)
2339+
2340+
2341+
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
2342+
@docstring.copy(Figure.waitforbuttonpress)
2343+
def waitforbuttonpress(timeout=-1):
2344+
return gcf().waitforbuttonpress(timeout=timeout)
2345+
2346+
23302347
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
23312348
@docstring.copy(Axes.acorr)
23322349
def acorr(x, *, data=None, **kwargs):

‎tools/boilerplate.py

Copy file name to clipboardExpand all lines: tools/boilerplate.py
+37-11Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import numpy as np
2323
from matplotlib import cbook, mlab
2424
from 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
@@ -34,20 +35,26 @@
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})
3940
def {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})
4748
def {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
5259
CMAP_TEMPLATE = AUTOGEN_MSG + '''
5360
def {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):
164174
def 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

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.