| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff 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. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -690,6 +690,9 @@ def limit_range_for_scale(self, vmin, vmax, minpos): | |||
| 690 | 690 | 'functionlog': FuncScaleLog, | |
| 691 | 691 | } | |
| 692 | 692 | ||
| 693 | + # caching of signature info | ||
| 694 | + _scale_has_axis_parameter = {} | ||
| 695 | + | ||
| 693 | 696 | ||
| 694 | 697 | def get_scale_names(): | |
| 695 | 698 | """Return the names of the available scales.""" | |
@@ -706,7 +709,19 @@ def scale_factory(scale, axis, **kwargs): | |||
| 706 | 709 | axis : `~matplotlib.axis.Axis` | |
| 707 | 710 | """ | |
| 708 | 711 | 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) | ||
| 710 | 725 | ||
| 711 | 726 | ||
| 712 | 727 | if scale_factory.__doc__: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,6 +8,7 @@ | |||
| 8 | 8 | import matplotlib.scale as mscale | |
| 9 | 9 | from matplotlib.ticker import AsinhLocator, LogFormatterSciNotation | |
| 10 | 10 | from matplotlib.testing.decorators import check_figures_equal, image_comparison | |
| 11 | + from matplotlib.transforms import IdentityTransform | ||
| 11 | 12 | ||
| 12 | 13 | import numpy as np | |
| 13 | 14 | from numpy.testing import assert_allclose | |
@@ -293,3 +294,29 @@ def test_bad_scale(self): | |||
| 293 | 294 | AsinhScale(axis=None, linear_width=-1) | |
| 294 | 295 | s0 = AsinhScale(axis=None, ) | |
| 295 | 296 | 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 | + name = "custom" | ||
| 308 | + | ||
| 309 | + def __init__(self): | ||
| 310 | + self._transform = CustomTransform() | ||
| 311 | + | ||
| 312 | + def get_transform(self): | ||
| 313 | + return self._transform | ||
| 314 | + | ||
| 315 | + try: | ||
| 316 | + mscale.register_scale(CustomScale) | ||
| 317 | + fig, ax = plt.subplots() | ||
| 318 | + ax.set_xscale('custom') | ||
| 319 | + assert isinstance(ax.xaxis.get_transform(), CustomTransform) | ||
| 320 | + finally: | ||
| 321 | + # cleanup - there's no public unregister_scale() | ||
| 322 | + del mscale._scale_mapping["custom"] | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments