Skip to content

Commit

Permalink
Merge pull request #481 from mdboom/symlog_bugs
Browse files Browse the repository at this point in the history
Implements a new version of symlog that is smooth around linthresh
  • Loading branch information
efiring committed Sep 23, 2011
2 parents 387d8fe + 10ea86c commit 187f403
Show file tree
Hide file tree
Showing 10 changed files with 2,997 additions and 2,146 deletions.
3 changes: 2 additions & 1 deletion doc/users/whats_new.rst
Expand Up @@ -191,7 +191,8 @@ Other improvements
* :meth:`~matplotlib.pyplot.scatter` now accepts empty inputs.

* The behavior for 'symlog' scale has been fixed, but this may result
in some minor changes to existing plots.
in some minor changes to existing plots. This work was refined by
ssyr.

* Peter Butterworth added named figure support to
:func:`~matplotlib.pyplot.figure`.
Expand Down
2 changes: 1 addition & 1 deletion examples/pylab_examples/symlog_demo.py
Expand Up @@ -21,7 +21,7 @@
subplot(313)
plot(x, np.sin(x / 3.0))
xscale('symlog')
yscale('symlog')
yscale('symlog', linthreshy=0.015)
grid(True)
ylabel('symlog both')

Expand Down
70 changes: 31 additions & 39 deletions lib/matplotlib/scale.py
Expand Up @@ -312,58 +312,50 @@ class SymmetricalLogScale(ScaleBase):
name = 'symlog'

class SymmetricalLogTransform(Transform):
input_dims = 1
output_dims = 1
is_separable = True

def __init__(self, base, linthresh):
Transform.__init__(self)
self.base = base
self.linthresh = linthresh
self._log_base = np.log(base)
self._linadjust = (np.log(linthresh) / self._log_base) / linthresh

def transform(self, a):
sign = np.sign(a)
masked = ma.masked_inside(a, -self.linthresh, self.linthresh, copy=False)
log = sign * self.linthresh * (1 + ma.log(np.abs(masked) / self.linthresh))
if masked.mask.any():
return ma.where(masked.mask, a, log)
else:
return log

def inverted(self):
return SymmetricalLogScale.InvertedSymmetricalLogTransform(self.base, self.linthresh)

class InvertedSymmetricalLogTransform(Transform):
input_dims = 1
output_dims = 1
is_separable = True

def __init__(self, base, linthresh):
Transform.__init__(self)
self.base = base
self.linthresh = abs(linthresh)
self.linthresh = linthresh
self._log_base = np.log(base)
logb_linthresh = np.log(linthresh) / self._log_base
self._linadjust = 1.0 - logb_linthresh
self._linscale = 1.0 / linthresh
self._log_linthresh = np.log(linthresh) / self._log_base
self._linadjust = linthresh / (np.log(linthresh) / self._log_base)

def transform(self, a):
a = np.asarray(a)
sign = np.sign(a)
masked = ma.masked_inside(a, -self.linthresh, self.linthresh, copy=False)
exp = sign * self.linthresh * ma.exp(sign * masked / self.linthresh - 1)
if masked.mask.any():
log = sign * (ma.log(np.abs(masked)) / self._log_base + self._linadjust)
return np.asarray(ma.where(masked.mask, a * self._linscale, log))
return ma.where(masked.mask, a, exp)
else:
return sign * (np.log(np.abs(a)) / self._log_base + self._linadjust)

def inverted(self):
return SymmetricalLogScale.InvertedSymmetricalLogTransform(
self.base, self.linthresh)

class InvertedSymmetricalLogTransform(Transform):
input_dims = 1
output_dims = 1
is_separable = True

def __init__(self, base, linthresh):
Transform.__init__(self)
self.base = base
self.linthresh = linthresh
log_base = np.log(base)
logb_linthresh = np.log(linthresh) / log_base
self._linadjust = 1.0 - logb_linthresh

def transform(self, a):
a = np.asarray(a)
sign = np.sign(a)
masked = ma.masked_inside(a, -1.0, 1.0, copy=False)
result = np.where((a >= -1.0) & (a <= 1.0),
a * self.linthresh,
sign * np.power(self.base, np.abs(a - sign * self._linadjust)))
return result

def inverted(self):
return SymmetricalLogScale.SymmetricalLogTransform(
self.base, self.linthresh)
return exp

def __init__(self, axis, **kwargs):
"""
Expand Down Expand Up @@ -395,7 +387,7 @@ def __init__(self, axis, **kwargs):

assert base > 0.0
assert linthresh > 0.0

self.base = base
self.linthresh = linthresh
self.subs = subs
Expand Down
Binary file modified lib/matplotlib/tests/baseline_images/test_axes/symlog.pdf
Binary file not shown.
Binary file modified lib/matplotlib/tests/baseline_images/test_axes/symlog.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 187f403

Please sign in to comment.