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

Commit6386d63

Browse files
committed
MNT: Registered 3rd party scales do not need an axis parameter anymore
First step of#29349.
1 parenta18354a commit6386d63

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
3rd party scales do not need to have an *axis* parameter anymore
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
Since matplotlib 3.1 `PR 12831<https://github.com/matplotlib/matplotlib/pull/12831>`_
5+
scales should be reusable and therefore independent of the Axis. Therefore the use of
6+
of the *axis* parameter in the ``__init__`` had been discouraged. However, that
7+
parameter was still necessary for API compatibility. This is no longer the case.
8+
9+
`.register_scale` now accepts scale classes with and without this parameter.
10+
3rd party scales can and should remove that parameter.

‎lib/matplotlib/scale.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,9 @@ def limit_range_for_scale(self, vmin, vmax, minpos):
690690
'functionlog':FuncScaleLog,
691691
}
692692

693+
# caching of signature info
694+
_scale_has_axis_parameter= {}
695+
693696

694697
defget_scale_names():
695698
"""Return the names of the available scales."""
@@ -706,7 +709,19 @@ def scale_factory(scale, axis, **kwargs):
706709
axis : `~matplotlib.axis.Axis`
707710
"""
708711
scale_cls=_api.check_getitem(_scale_mapping,scale=scale)
709-
returnscale_cls(axis,**kwargs)
712+
713+
# We support scales that may or may not have an initial *axis* parameter.
714+
# This information is cached, so that we do not need to inspect the signature
715+
# on every time we create a scale.
716+
ifscalenotin_scale_has_axis_parameter:
717+
_scale_has_axis_parameter[scale]= (
718+
"axis"ininspect.signature(scale_cls).parameters
719+
)
720+
721+
if_scale_has_axis_parameter[scale]:
722+
returnscale_cls(axis,**kwargs)
723+
else:
724+
returnscale_cls(**kwargs)
710725

711726

712727
ifscale_factory.__doc__:

‎lib/matplotlib/tests/test_scale.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
importmatplotlib.scaleasmscale
99
frommatplotlib.tickerimportAsinhLocator,LogFormatterSciNotation
1010
frommatplotlib.testing.decoratorsimportcheck_figures_equal,image_comparison
11+
frommatplotlib.transformsimportIdentityTransform
1112

1213
importnumpyasnp
1314
fromnumpy.testingimportassert_allclose
@@ -293,3 +294,27 @@ def test_bad_scale(self):
293294
AsinhScale(axis=None,linear_width=-1)
294295
s0=AsinhScale(axis=None, )
295296
s1=AsinhScale(axis=None,linear_width=3.0)
297+
298+
299+
deftest_custom_scale_without_axis():
300+
"""
301+
Test that one can register and use custom scales that don't take an *axis* param.
302+
"""
303+
classCustomTransform(IdentityTransform):
304+
pass
305+
306+
classCustomScale(mscale.ScaleBase):
307+
def__init__(self):
308+
self._transform=CustomTransform()
309+
310+
defget_transform(self):
311+
returnself._transform
312+
313+
try:
314+
mscale.register_scale("custom",CustomScale)
315+
fig,ax=plt.subplots()
316+
ax.set_xscale('custom')
317+
assertisinstance(ax.xaxis.get_transform(),CustomTransform)
318+
finally:
319+
# cleanup - there's no public unregister_scale()
320+
delmscale._scale_mapping["custom"]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp