Skip to content

Navigation Menu

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 6386d63

Browse filesBrowse files
committed
MNT: Registered 3rd party scales do not need an axis parameter anymore
First step of #29349.
1 parent a18354a commit 6386d63
Copy full SHA for 6386d63

File tree

3 files changed

+51
-1
lines changed
Filter options

3 files changed

+51
-1
lines changed
+10Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/scale.py
+16-1Lines 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
def get_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-
return scale_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+
if scale not in _scale_has_axis_parameter:
717+
_scale_has_axis_parameter[scale] = (
718+
"axis" in inspect.signature(scale_cls).parameters
719+
)
720+
721+
if _scale_has_axis_parameter[scale]:
722+
return scale_cls(axis, **kwargs)
723+
else:
724+
return scale_cls(**kwargs)
710725

711726

712727
if scale_factory.__doc__:

‎lib/matplotlib/tests/test_scale.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_scale.py
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import matplotlib.scale as mscale
99
from matplotlib.ticker import AsinhLocator, LogFormatterSciNotation
1010
from matplotlib.testing.decorators import check_figures_equal, image_comparison
11+
from matplotlib.transforms import IdentityTransform
1112

1213
import numpy as np
1314
from numpy.testing import assert_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+
def test_custom_scale_without_axis():
300+
"""
301+
Test that one can register and use custom scales that don't take an *axis* param.
302+
"""
303+
class CustomTransform(IdentityTransform):
304+
pass
305+
306+
class CustomScale(mscale.ScaleBase):
307+
def __init__(self):
308+
self._transform = CustomTransform()
309+
310+
def get_transform(self):
311+
return self._transform
312+
313+
try:
314+
mscale.register_scale("custom", CustomScale)
315+
fig, ax = plt.subplots()
316+
ax.set_xscale('custom')
317+
assert isinstance(ax.xaxis.get_transform(), CustomTransform)
318+
finally:
319+
# cleanup - there's no public unregister_scale()
320+
del mscale._scale_mapping["custom"]

0 commit comments

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