@@ -1194,7 +1194,7 @@ class SymLogNorm(Normalize):
1194
1194
*linthresh* allows the user to specify the size of this range
1195
1195
(-*linthresh*, *linthresh*).
1196
1196
"""
1197
- def __init__ (self , linthresh , linscale = 1.0 ,
1197
+ def __init__ (self , linthresh , linscale = 1.0 ,
1198
1198
vmin = None , vmax = None , clip = False ):
1199
1199
"""
1200
1200
*linthresh*:
@@ -1212,9 +1212,10 @@ def __init__(self, linthresh, linscale=1.0,
1212
1212
"""
1213
1213
Normalize .__init__ (self , vmin , vmax , clip )
1214
1214
self .linthresh = float (linthresh )
1215
- self ._linscale_adj = (linscale / (1.0 - np .e ** - 1 ))
1216
- if vmin is not None and vmax is not None :
1217
- self ._transform_vmin_vmax ()
1215
+ # Number of decades in the log region
1216
+ ndec = np .log10 (vmax / linthresh )
1217
+ # Size of the linear region from 0 to linthresh in the transformed space
1218
+ self .linear_size = 1 / (1 + linscale / ndec )
1218
1219
1219
1220
def __call__ (self , value , clip = None ):
1220
1221
if clip is None :
@@ -1235,57 +1236,52 @@ def __call__(self, value, clip=None):
1235
1236
mask = mask )
1236
1237
# in-place equivalent of above can be much faster
1237
1238
resdat = self ._transform (result .data )
1238
- resdat -= self ._lower
1239
- resdat /= (self ._upper - self ._lower )
1240
1239
1241
1240
if is_scalar :
1242
1241
result = result [0 ]
1243
1242
return result
1244
1243
1245
1244
def _transform (self , a ):
1246
- """Inplace transformation. """
1245
+ """In-place mapping from *a* to [0, 1] """
1247
1246
with np .errstate (invalid = "ignore" ):
1248
- masked = np .abs (a ) > self .linthresh
1249
- sign = np .sign (a [masked ])
1250
- log = (self ._linscale_adj + np .log (np .abs (a [masked ]) / self .linthresh ))
1251
- log *= sign * self .linthresh
1252
- a [masked ] = log
1253
- a [~ masked ] *= self ._linscale_adj
1247
+ logregion = np .abs (a ) > self .linthresh
1248
+
1249
+ # Transform log value
1250
+ sign = np .sign (a [logregion ])
1251
+ log = (1 - self .linear_size ) * np .log10 (np .abs (a [logregion ])) + self .linear_size
1252
+ a [logregion ] = log * sign
1253
+
1254
+ # Transform linear values
1255
+ a [~ logregion ] *= self .linear_size / self .linthresh
1256
+
1257
+ # Transform from [-1, 1] to [0, 1]
1258
+ a += 1
1259
+ a /= 2
1254
1260
return a
1255
1261
1256
1262
def _inv_transform (self , a ):
1257
1263
"""Inverse inplace Transformation."""
1258
- masked = np .abs (a ) > (self .linthresh * self ._linscale_adj )
1259
- sign = np .sign (a [masked ])
1260
- exp = np .exp (sign * a [masked ] / self .linthresh - self ._linscale_adj )
1261
- exp *= sign * self .linthresh
1262
- a [masked ] = exp
1263
- a [~ masked ] /= self ._linscale_adj
1264
+ # Transform from [0, 1] to [-1, 1]
1265
+ a *= 2
1266
+ a -= 1
1267
+
1268
+ # Transform back log values
1269
+ logregion = np .abs (a ) > self .linear_size
1270
+ sign = np .sign (a [logregion ])
1271
+ exp = 10 ** ((np .abs (a [logregion ]) - self .linear_size ) /
1272
+ (1 - self .linear_size ))
1273
+ a [logregion ] = exp * sign
1274
+
1275
+ # Transform back linear values
1276
+ a [~ logregion ] /= self .linear_size / self .linthresh
1264
1277
return a
1265
1278
1266
- def _transform_vmin_vmax (self ):
1267
- """Calculates vmin and vmax in the transformed system."""
1268
- vmin , vmax = self .vmin , self .vmax
1269
- arr = np .array ([vmax , vmin ]).astype (float )
1270
- self ._upper , self ._lower = self ._transform (arr )
1271
-
1272
1279
def inverse (self , value ):
1273
1280
if not self .scaled ():
1274
1281
raise ValueError ("Not invertible until scaled" )
1275
1282
val = np .ma .asarray (value )
1276
- val = val * (self ._upper - self ._lower ) + self ._lower
1277
1283
return self ._inv_transform (val )
1278
1284
1279
- def autoscale (self , A ):
1280
- # docstring inherited.
1281
- super ().autoscale (A )
1282
- self ._transform_vmin_vmax ()
1283
-
1284
- def autoscale_None (self , A ):
1285
- # docstring inherited.
1286
- super ().autoscale_None (A )
1287
- self ._transform_vmin_vmax ()
1288
-
1289
1285
1290
1286
class PowerNorm (Normalize ):
1291
1287
"""
0 commit comments