diff --git a/lib/matplotlib/scale.py b/lib/matplotlib/scale.py index d9026a619212..9d1054fd9a86 100644 --- a/lib/matplotlib/scale.py +++ b/lib/matplotlib/scale.py @@ -93,7 +93,7 @@ class LogTransformBase(Transform): is_separable = True has_inverse = True - def __init__(self, nonpos): + def __init__(self, nonpos='clip'): Transform.__init__(self) self._clip = {"clip": True, "mask": False}[nonpos] @@ -114,6 +114,10 @@ def transform_non_affine(self, a): out[a <= 0] = -1000 return out + def __str__(self): + return "{}({!r})".format(type(self).__name__, + "clip" if self._clip else "mask") + class InvertedLogTransformBase(Transform): input_dims = 1 @@ -124,6 +128,9 @@ class InvertedLogTransformBase(Transform): def transform_non_affine(self, a): return ma.power(self.base, a) + def __str__(self): + return "{}()".format(type(self).__name__) + class Log10Transform(LogTransformBase): base = 10.0 @@ -168,7 +175,7 @@ def inverted(self): class LogTransform(LogTransformBase): - def __init__(self, base, nonpos): + def __init__(self, base, nonpos='clip'): LogTransformBase.__init__(self, nonpos) self.base = base @@ -448,7 +455,7 @@ class LogitTransform(Transform): is_separable = True has_inverse = True - def __init__(self, nonpos): + def __init__(self, nonpos='mask'): Transform.__init__(self) self._nonpos = nonpos self._clip = {"clip": True, "mask": False}[nonpos] @@ -465,6 +472,10 @@ def transform_non_affine(self, a): def inverted(self): return LogisticTransform(self._nonpos) + def __str__(self): + return "{}({!r})".format(type(self).__name__, + "clip" if self._clip else "mask") + class LogisticTransform(Transform): input_dims = 1 @@ -483,6 +494,9 @@ def transform_non_affine(self, a): def inverted(self): return LogitTransform(self._nonpos) + def __str__(self): + return "{}({!r})".format(type(self).__name__, self._nonpos) + class LogitScale(ScaleBase): """ diff --git a/lib/matplotlib/tests/test_scale.py b/lib/matplotlib/tests/test_scale.py index 9b78377b26bc..c50a0a89609c 100644 --- a/lib/matplotlib/tests/test_scale.py +++ b/lib/matplotlib/tests/test_scale.py @@ -1,7 +1,8 @@ -from __future__ import print_function +from __future__ import print_function, unicode_literals from matplotlib.testing.decorators import image_comparison import matplotlib.pyplot as plt +from matplotlib.scale import Log10Transform, InvertedLog10Transform import numpy as np import io import pytest @@ -74,6 +75,27 @@ def test_extra_kwargs_raise(): ax.set_yscale('log', nonpos='mask') +def test_logscale_invert_transform(): + fig, ax = plt.subplots() + ax.set_yscale('log') + # get transformation from data to axes + tform = (ax.transAxes + ax.transData.inverted()).inverted() + + # direct test of log transform inversion + assert isinstance(Log10Transform().inverted(), InvertedLog10Transform) + + +def test_logscale_transform_repr(): + # check that repr of log transform succeeds + fig, ax = plt.subplots() + ax.set_yscale('log') + s = repr(ax.transData) + + # check that repr of log transform returns correct string + s = repr(Log10Transform(nonpos='clip')) + assert s == "Log10Transform({!r})".format('clip') + + @image_comparison(baseline_images=['logscale_nonpos_values'], remove_text=True, extensions=['png'], style='mpl20') def test_logscale_nonpos_values():