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

Commiteed5b94

Browse files
committed
FuncFormatter can take func(x) and func(x,pos), and FuncFormatter tests
1 parent100c7cf commiteed5b94

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

‎lib/matplotlib/tests/test_ticker.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,27 @@ def test_basic(self):
10411041
assert'00002'==tmp_form(2)
10421042

10431043

1044+
classTestFuncFormatter:
1045+
deftest_input_x_pos(self):
1046+
func=lambdax,pos :f"{x}+{pos}"
1047+
funcform=mticker.FuncFormatter(func)
1048+
assert"1+2"==funcform(1,2)
1049+
1050+
deftext_input_x(self):
1051+
func=lambdax:f"hello{x}"
1052+
funcform=mticker.FuncFormatter(func)
1053+
assert"hello 0"==funcform(0)
1054+
1055+
deftest_error(self):
1056+
withpytest.raises(TypeError,match=r"FuncFormatter*"):
1057+
func=lambdax,y,z:" ".join([x,y,z])
1058+
funcform=mticker.FuncFormatter(func)
1059+
funcform(1,2,3)
1060+
1061+
deftest_built_in(self):
1062+
funcform=mticker.FuncFormatter("{} hi!".format)
1063+
assert"42 hi!"==funcform(42)
1064+
10441065
classTestStrMethodFormatter:
10451066
test_data= [
10461067
('{x:05d}', (2,),'00002'),

‎lib/matplotlib/ticker.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@
168168
importlogging
169169
importlocale
170170
importmath
171+
importinspect
171172
fromnumbersimportIntegral
172173

173174
importnumpyasnp
@@ -377,19 +378,34 @@ class FuncFormatter(Formatter):
377378
"""
378379
Use a user-defined function for formatting.
379380
380-
The functionshould take in two inputs (a tick value ``x`` anda
381-
position ``pos``), and return a string containing thecorresponding
382-
tick label.
381+
The functioncan take inat mosttwo inputs (arequiredtick value ``x`` and
382+
an optionalposition ``pos``), andmustreturn a string containing the
383+
correspondingtick label.
383384
"""
384385
def__init__(self,func):
385-
self.func=func
386+
try:
387+
self.nargs=len(inspect.signature(func).parameters)
388+
ifself.nargs==2:
389+
self.func=func
390+
elifself.nargs==1:
391+
deffunc_pos(x,pos):
392+
returnfunc(x)
393+
self.func=func_pos
394+
else:
395+
raiseTypeError("""FuncFormatter functions take at most
396+
2 parameters: x (required), pos (optional)""")
397+
exceptValueErrorase:
398+
#built ins like str.format don't have signatures
399+
self.func=func
400+
386401

387402
def__call__(self,x,pos=None):
388403
"""
389404
Return the value of the user defined function.
390405
391406
*x* and *pos* are passed through as-is.
392407
"""
408+
393409
returnself.func(x,pos)
394410

395411

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp