|
31 | 31 |
|
32 | 32 | import inspect
|
33 | 33 | import textwrap
|
| 34 | +from functools import wraps |
34 | 35 |
|
35 | 36 | import numpy as np
|
36 | 37 |
|
@@ -103,13 +104,61 @@ def limit_range_for_scale(self, vmin, vmax, minpos):
|
103 | 104 | return vmin, vmax
|
104 | 105 |
|
105 | 106 |
|
| 107 | +def _make_axis_parameter_optional(init_func): |
| 108 | + """ |
| 109 | + Decorator to allow leaving out the *axis* parameter in scale constructors. |
| 110 | +
|
| 111 | + This decorator ensures backward compatibility for scale classes that |
| 112 | + previously required an *axis* parameter. It allows constructors to be |
| 113 | + callerd with or without the *axis* parameter. |
| 114 | +
|
| 115 | + For simplicity, this does not handle the case when *axis* |
| 116 | + is passed as a keyword. However, |
| 117 | + scanning GitHub, there's no evidence that that is used anywhere. |
| 118 | +
|
| 119 | + Parameters |
| 120 | + ---------- |
| 121 | + init_func : callable |
| 122 | + The original __init__ method of a scale class. |
| 123 | +
|
| 124 | + Returns |
| 125 | + ------- |
| 126 | + callable |
| 127 | + A wrapped version of *init_func* that handles the optional *axis*. |
| 128 | +
|
| 129 | + Notes |
| 130 | + ----- |
| 131 | + If the wrapped constructor defines *axis* as its first argument, the |
| 132 | + parameter is preserved when present. Otherwise, the value `None` is injected |
| 133 | + as the first argument. |
| 134 | +
|
| 135 | + Examples |
| 136 | + -------- |
| 137 | + >>> from matplotlib.scale import ScaleBase |
| 138 | + >>> class CustomScale(ScaleBase): |
| 139 | + ... @_make_axis_parameter_optional |
| 140 | + ... def __init__(self, axis, custom_param=1): |
| 141 | + ... self.custom_param = custom_param |
| 142 | + """ |
| 143 | + @wraps(init_func) |
| 144 | + def wrapper(self, *args, **kwargs): |
| 145 | + if args and isinstance(args[0], mpl.axis.Axis): |
| 146 | + return init_func(self, *args, **kwargs) |
| 147 | + else: |
| 148 | + # Remove 'axis' from kwargs to avoid double assignment |
| 149 | + axis = kwargs.pop('axis', None) |
| 150 | + return init_func(self, axis, *args, **kwargs) |
| 151 | + return wrapper |
| 152 | + |
| 153 | + |
106 | 154 | class LinearScale(ScaleBase):
|
107 | 155 | """
|
108 | 156 | The default linear scale.
|
109 | 157 | """
|
110 | 158 |
|
111 | 159 | name = 'linear'
|
112 | 160 |
|
| 161 | + @_make_axis_parameter_optional |
113 | 162 | def __init__(self, axis):
|
114 | 163 | # This method is present only to prevent inheritance of the base class'
|
115 | 164 | # constructor docstring, which would otherwise end up interpolated into
|
@@ -180,6 +229,7 @@ class FuncScale(ScaleBase):
|
180 | 229 |
|
181 | 230 | name = 'function'
|
182 | 231 |
|
| 232 | + @_make_axis_parameter_optional |
183 | 233 | def __init__(self, axis, functions):
|
184 | 234 | """
|
185 | 235 | Parameters
|
@@ -279,7 +329,8 @@ class LogScale(ScaleBase):
|
279 | 329 | """
|
280 | 330 | name = 'log'
|
281 | 331 |
|
282 |
| - def __init__(self, axis, *, base=10, subs=None, nonpositive="clip"): |
| 332 | + @_make_axis_parameter_optional |
| 333 | + def __init__(self, axis=None, *, base=10, subs=None, nonpositive="clip"): |
283 | 334 | """
|
284 | 335 | Parameters
|
285 | 336 | ----------
|
@@ -330,6 +381,7 @@ class FuncScaleLog(LogScale):
|
330 | 381 |
|
331 | 382 | name = 'functionlog'
|
332 | 383 |
|
| 384 | + @_make_axis_parameter_optional |
333 | 385 | def __init__(self, axis, functions, base=10):
|
334 | 386 | """
|
335 | 387 | Parameters
|
@@ -455,7 +507,8 @@ class SymmetricalLogScale(ScaleBase):
|
455 | 507 | """
|
456 | 508 | name = 'symlog'
|
457 | 509 |
|
458 |
| - def __init__(self, axis, *, base=10, linthresh=2, subs=None, linscale=1): |
| 510 | + @_make_axis_parameter_optional |
| 511 | + def __init__(self, axis=None, *, base=10, linthresh=2, subs=None, linscale=1): |
459 | 512 | self._transform = SymmetricalLogTransform(base, linthresh, linscale)
|
460 | 513 | self.subs = subs
|
461 | 514 |
|
@@ -547,7 +600,8 @@ class AsinhScale(ScaleBase):
|
547 | 600 | 1024: (256, 512)
|
548 | 601 | }
|
549 | 602 |
|
550 |
| - def __init__(self, axis, *, linear_width=1.0, |
| 603 | + @_make_axis_parameter_optional |
| 604 | + def __init__(self, axis=None, *, linear_width=1.0, |
551 | 605 | base=10, subs='auto', **kwargs):
|
552 | 606 | """
|
553 | 607 | Parameters
|
@@ -645,7 +699,8 @@ class LogitScale(ScaleBase):
|
645 | 699 | """
|
646 | 700 | name = 'logit'
|
647 | 701 |
|
648 |
| - def __init__(self, axis, nonpositive='mask', *, |
| 702 | + @_make_axis_parameter_optional |
| 703 | + def __init__(self, axis=None, nonpositive='mask', *, |
649 | 704 | one_half=r"\frac{1}{2}", use_overline=False):
|
650 | 705 | r"""
|
651 | 706 | Parameters
|
|
0 commit comments