Skip to content

Fix InvertedLog10Transform.inverted() #10242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions lib/matplotlib/scale.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -448,7 +455,7 @@ class LogitTransform(Transform):
is_separable = True
has_inverse = True

def __init__(self, nonpos):
def __init__(self, nonpos='mask'):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs changing to 'clip' instead of 'mask'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency, I used the same default for LogitTransform (and LogisticTransform) as for LogitScale, which already uses 'mask'.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Transform.__init__(self)
self._nonpos = nonpos
self._clip = {"clip": True, "mask": False}[nonpos]
Expand All @@ -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
Expand All @@ -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):
"""
Expand Down
24 changes: 23 additions & 1 deletion lib/matplotlib/tests/test_scale.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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():
Expand Down