1
+ """
2
+ Scales define the distribution of data values on an axis, e.g. a log scaling.
3
+
4
+ They are attached to an `~.axis.Axis` and hold a `.Transform`, which is
5
+ responsible for the actual data transformation.
6
+
7
+ See also `.axes.Axes.set_xscale` and the scales examples in the documentation.
8
+ """
9
+
1
10
import inspect
2
11
import textwrap
3
12
@@ -20,12 +29,14 @@ class ScaleBase:
20
29
21
30
Any subclasses will want to override:
22
31
23
- - :attr:`name`
24
- - :meth:`get_transform`
25
- - :meth:`set_default_locators_and_formatters`
32
+ - :attr:`name`
33
+ - :meth:`get_transform`
34
+ - :meth:`set_default_locators_and_formatters`
26
35
27
36
And optionally:
28
- - :meth:`limit_range_for_scale`
37
+
38
+ - :meth:`limit_range_for_scale`
39
+
29
40
"""
30
41
31
42
def __init__ (self , axis , ** kwargs ):
@@ -51,9 +62,8 @@ def get_transform(self):
51
62
52
63
def set_default_locators_and_formatters (self , axis ):
53
64
"""
54
- Set the :class:`~matplotlib.ticker.Locator` and
55
- :class:`~matplotlib.ticker.Formatter` objects on the given
56
- axis to match this scale.
65
+ Set the locators and formatters of *axis* to instances suitable for
66
+ this scale.
57
67
"""
58
68
raise NotImplementedError ()
59
69
@@ -63,7 +73,7 @@ def limit_range_for_scale(self, vmin, vmax, minpos):
63
73
domain supported by this scale.
64
74
65
75
*minpos* should be the minimum positive value in the data.
66
- This is used by log scales to determine a minimum value.
76
+ This is used by log scales to determine a minimum value.
67
77
"""
68
78
return vmin , vmax
69
79
@@ -84,10 +94,7 @@ def __init__(self, axis, **kwargs):
84
94
super ().__init__ (axis , ** kwargs )
85
95
86
96
def set_default_locators_and_formatters (self , axis ):
87
- """
88
- Set the locators and formatters to reasonable defaults for
89
- linear scaling.
90
- """
97
+ # docstring inherited
91
98
axis .set_major_locator (AutoLocator ())
92
99
axis .set_major_formatter (ScalarFormatter ())
93
100
axis .set_minor_formatter (NullFormatter ())
@@ -100,8 +107,8 @@ def set_default_locators_and_formatters(self, axis):
100
107
101
108
def get_transform (self ):
102
109
"""
103
- The transform for linear scaling is just the
104
- :class: `~matplotlib.transforms.IdentityTransform`.
110
+ Return the transform for linear scaling, which is just the
111
+ `~matplotlib.transforms.IdentityTransform`.
105
112
"""
106
113
return IdentityTransform ()
107
114
@@ -157,8 +164,8 @@ def __init__(self, axis, functions):
157
164
"""
158
165
Parameters
159
166
----------
160
- axis: the axis for the scale
161
-
167
+ axis : `~matplotlib. axis.Axis`
168
+ The axis for the scale.
162
169
functions : (callable, callable)
163
170
two-tuple of the forward and inverse functions for the scale.
164
171
The forward function must be monotonic.
@@ -172,16 +179,11 @@ def forward(values: array-like) -> array-like
172
179
self ._transform = transform
173
180
174
181
def get_transform (self ):
175
- """
176
- The transform for arbitrary scaling
177
- """
182
+ """Return the `.FuncTransform` associated with this scale."""
178
183
return self ._transform
179
184
180
185
def set_default_locators_and_formatters (self , axis ):
181
- """
182
- Set the locators and formatters to the same defaults as the
183
- linear scale.
184
- """
186
+ # docstring inherited
185
187
axis .set_major_locator (AutoLocator ())
186
188
axis .set_major_formatter (ScalarFormatter ())
187
189
axis .set_minor_formatter (NullFormatter ())
@@ -293,7 +295,7 @@ def transform_non_affine(self, a):
293
295
# Ignore invalid values due to nans being passed to the transform.
294
296
with np .errstate (divide = "ignore" , invalid = "ignore" ):
295
297
log = {np .e : np .log , 2 : np .log2 , 10 : np .log10 }.get (self .base )
296
- if log : # If possible, do everything in a single call to Numpy .
298
+ if log : # If possible, do everything in a single call to NumPy .
297
299
out = log (a )
298
300
else :
299
301
out = np .log (a )
@@ -354,20 +356,20 @@ class LogScale(ScaleBase):
354
356
355
357
def __init__ (self , axis , ** kwargs ):
356
358
"""
357
- *basex*/*basey*:
358
- The base of the logarithm
359
-
360
- *nonposx*/*nonposy*: {'mask', 'clip'}
361
- non-positive values in *x* or *y* can be masked as
362
- invalid, or clipped to a very small positive number
363
-
364
- *subsx*/*subsy*:
365
- Where to place the subticks between each major tick .
366
- Should be a sequence of integers. For example, in a log10
367
- scale: ``[2, 3, 4, 5, 6, 7, 8, 9]``
368
-
369
- will place 8 logarithmically spaced minor ticks between
370
- each major tick.
359
+ Parameters
360
+ ----------
361
+ axis : `~matplotlib.axis.Axis`
362
+ The axis for the scale.
363
+ basex, basey : float, default: 10
364
+ The base of the logarithm.
365
+ nonposx, nonposy : {'clip', 'mask'}, default: 'clip'
366
+ Determines the behavior for non-positive values. They can either
367
+ be masked as invalid, or clipped to a very small positive number .
368
+ subsx, subsy : sequence of int, default: None
369
+ Where to place the subticks between each major tick.
370
+ For example, in a log10 scale: ``[2, 3, 4, 5, 6, 7, 8, 9]``
371
+ will place 8 logarithmically spaced minor ticks between
372
+ each major tick.
371
373
"""
372
374
if axis .axis_name == 'x' :
373
375
base = kwargs .pop ('basex' , 10.0 )
@@ -397,10 +399,7 @@ def base(self):
397
399
return self ._transform .base
398
400
399
401
def set_default_locators_and_formatters (self , axis ):
400
- """
401
- Set the locators and formatters to specialized versions for
402
- log scaling.
403
- """
402
+ # docstring inherited
404
403
axis .set_major_locator (LogLocator (self .base ))
405
404
axis .set_major_formatter (LogFormatterSciNotation (self .base ))
406
405
axis .set_minor_locator (LogLocator (self .base , self .subs ))
@@ -409,16 +408,11 @@ def set_default_locators_and_formatters(self, axis):
409
408
labelOnlyBase = (self .subs is not None )))
410
409
411
410
def get_transform (self ):
412
- """
413
- Return a :class:`~matplotlib.transforms.Transform` instance
414
- appropriate for the given logarithm base.
415
- """
411
+ """Return the `.LogTransform` associated with this scale."""
416
412
return self ._transform
417
413
418
414
def limit_range_for_scale (self , vmin , vmax , minpos ):
419
- """
420
- Limit the domain to positive values.
421
- """
415
+ """Limit the domain to positive values."""
422
416
if not np .isfinite (minpos ):
423
417
minpos = 1e-300 # Should rarely (if ever) have a visible effect.
424
418
@@ -438,8 +432,8 @@ def __init__(self, axis, functions, base=10):
438
432
"""
439
433
Parameters
440
434
----------
441
- axis: the axis for the scale
442
-
435
+ axis : `matplotlib. axis.Axis`
436
+ The axis for the scale.
443
437
functions : (callable, callable)
444
438
two-tuple of the forward and inverse functions for the scale.
445
439
The forward function must be monotonic.
@@ -461,9 +455,7 @@ def base(self):
461
455
return self ._transform ._b .base # Base of the LogTransform.
462
456
463
457
def get_transform (self ):
464
- """
465
- The transform for arbitrary scaling
466
- """
458
+ """Return the `.Transform` associated with this scale."""
467
459
return self ._transform
468
460
469
461
@@ -592,20 +584,15 @@ def __init__(self, axis, **kwargs):
592
584
self .subs = subs
593
585
594
586
def set_default_locators_and_formatters (self , axis ):
595
- """
596
- Set the locators and formatters to specialized versions for
597
- symmetrical log scaling.
598
- """
587
+ # docstring inherited
599
588
axis .set_major_locator (SymmetricalLogLocator (self .get_transform ()))
600
589
axis .set_major_formatter (LogFormatterSciNotation (self .base ))
601
590
axis .set_minor_locator (SymmetricalLogLocator (self .get_transform (),
602
591
self .subs ))
603
592
axis .set_minor_formatter (NullFormatter ())
604
593
605
594
def get_transform (self ):
606
- """
607
- Return a :class:`SymmetricalLogTransform` instance.
608
- """
595
+ """Return the `.SymmetricalLogTransform` associated with this scale."""
609
596
return self ._transform
610
597
611
598
@@ -669,19 +656,23 @@ class LogitScale(ScaleBase):
669
656
670
657
def __init__ (self , axis , nonpos = 'mask' ):
671
658
"""
672
- *nonpos*: {'mask', 'clip'}
673
- values beyond ]0, 1[ can be masked as invalid, or clipped to a number
674
- very close to 0 or 1
659
+ Parameters
660
+ ----------
661
+ axis : `matplotlib.axis.Axis`
662
+ Currently unused.
663
+ nonpos : {'mask', 'clip'}
664
+ Determines the behavior for values beyond the open interval ]0, 1[.
665
+ They can either be masked as invalid, or clipped to a number very
666
+ close to 0 or 1.
675
667
"""
676
668
self ._transform = LogitTransform (nonpos )
677
669
678
670
def get_transform (self ):
679
- """
680
- Return a :class:`LogitTransform` instance.
681
- """
671
+ """Return the `.LogitTransform` associated with this scale."""
682
672
return self ._transform
683
673
684
674
def set_default_locators_and_formatters (self , axis ):
675
+ # docstring inherited
685
676
# ..., 0.01, 0.1, 0.5, 0.9, 0.99, ...
686
677
axis .set_major_locator (LogitLocator ())
687
678
axis .set_major_formatter (LogitFormatter ())
@@ -709,6 +700,7 @@ def limit_range_for_scale(self, vmin, vmax, minpos):
709
700
710
701
711
702
def get_scale_names ():
703
+ """Return the names of the available scales."""
712
704
return sorted (_scale_mapping )
713
705
714
706
@@ -719,22 +711,26 @@ def scale_factory(scale, axis, **kwargs):
719
711
Parameters
720
712
----------
721
713
scale : {%(names)s}
722
- axis : Axis
714
+ axis : `matplotlib.axis. Axis`
723
715
"""
724
716
scale = scale .lower ()
725
717
cbook ._check_in_list (_scale_mapping , scale = scale )
726
718
return _scale_mapping [scale ](axis , ** kwargs )
727
719
720
+
728
721
if scale_factory .__doc__ :
729
722
scale_factory .__doc__ = scale_factory .__doc__ % {
730
- "names" : ", " .join (get_scale_names ())}
723
+ "names" : ", " .join (map ( repr , get_scale_names () ))}
731
724
732
725
733
726
def register_scale (scale_class ):
734
727
"""
735
728
Register a new kind of scale.
736
729
737
- *scale_class* must be a subclass of :class:`ScaleBase`.
730
+ Parameters
731
+ ----------
732
+ scale_class : subclass of `ScaleBase`
733
+ The scale to register.
738
734
"""
739
735
_scale_mapping [scale_class .name ] = scale_class
740
736
0 commit comments