Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit48c7258

Browse files
authored
Merge pull request#13811 from timhoffm/boilerplate-figure
Generate Figure method wrappers via boilerplate.py
2 parents019aeea +29f1c87 commit48c7258

File tree

2 files changed

+79
-36
lines changed

2 files changed

+79
-36
lines changed

‎lib/matplotlib/pyplot.py

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -700,34 +700,9 @@ def savefig(*args, **kwargs):
700700
returnres
701701

702702

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

715705

716-
@docstring.copy(Figure.text)
717-
deffigtext(x,y,s,*args,**kwargs):
718-
returngcf().text(x,y,s,*args,**kwargs)
719-
720-
721-
@docstring.copy(Figure.suptitle)
722-
defsuptitle(t,**kwargs):
723-
returngcf().suptitle(t,**kwargs)
724-
725-
726-
@docstring.copy(Figure.figimage)
727-
deffigimage(*args,**kwargs):
728-
returngcf().figimage(*args,**kwargs)
729-
730-
731706
deffiglegend(*args,**kwargs):
732707
returngcf().legend(*args,**kwargs)
733708
ifFigure.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+
deffigimage(
2308+
X,xo=0,yo=0,alpha=None,norm=None,cmap=None,vmin=None,
2309+
vmax=None,origin=None,resize=False,**kwargs):
2310+
returngcf().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+
deffigtext(
2318+
x,y,s,fontdict=None,
2319+
withdash=cbook.deprecation._deprecated_parameter,**kwargs):
2320+
returngcf().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+
defginput(
2327+
n=1,timeout=30,show_clicks=True,mouse_add=1,mouse_pop=3,
2328+
mouse_stop=2):
2329+
returngcf().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+
defsuptitle(t,**kwargs):
2338+
returngcf().suptitle(t,**kwargs)
2339+
2340+
2341+
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
2342+
@docstring.copy(Figure.waitforbuttonpress)
2343+
defwaitforbuttonpress(timeout=-1):
2344+
returngcf().waitforbuttonpress(timeout=timeout)
2345+
2346+
23302347
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
23312348
@docstring.copy(Axes.acorr)
23322349
defacorr(x,*,data=None,**kwargs):

‎tools/boilerplate.py

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
importnumpyasnp
2323
frommatplotlibimportcbook,mlab
2424
frommatplotlib.axesimportAxes
25+
frommatplotlib.figureimportFigure
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
returnself._repr
8592

8693

87-
defgenerate_function(name,called_name,template,**kwargs):
94+
defgenerate_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-
Thefunction to be wrapped.
102+
called_fullname : str
103+
Themethod 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
ifMAX_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-
forreservedin ('gca','gci','__ret'):
161+
forreservedin ('gca','gci','gcf','__ret'):
152162
ifreservedinparams:
153163
raiseValueError(
154-
f'Axes method{called_name} has kwarg named{reserved}')
164+
f'Method{called_fullname} has kwarg named{reserved}')
155165

156166
returntemplate.format(
157167
name=name,
@@ -164,6 +174,14 @@ def generate_function(name, called_name, template, **kwargs):
164174
defboilerplate_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+
forspecin_figure_commands:
283+
if':'inspec:
284+
name,called_name=spec.split(':')
285+
else:
286+
name=called_name=spec
287+
yieldgenerate_function(name,f'Figure.{called_name}',
288+
FIGURE_METHOD_TEMPLATE)
289+
264290
forspecin_axes_commands:
265291
if':'inspec:
266292
name,called_name=spec.split(':')
267293
else:
268294
name=called_name=spec
269295

270-
template= (CMAPPABLE_TEMPLATEifnameincmappableelse
271-
NON_CMAPPABLE_TEMPLATE)
272-
yieldgenerate_function(name,called_name,template,
296+
template= (AXES_CMAPPABLE_METHOD_TEMPLATEifnameincmappableelse
297+
AXES_METHOD_TEMPLATE)
298+
yieldgenerate_function(name,f'Axes.{called_name}',template,
273299
sci_command=cmappable.get(name))
274300

275301
cmaps= (

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp