Convert x value to symlog scale with zero shift using Python and matplotlib.pyplot


The result is:

Convert x value to symlog scale with zero shift using Python and matplotlib.pyplot

This code shows how to convert x value to symlog scale with zero shift using Python and matplotlib.pyplot

See also:

Python Matplotlib Tips: Plot 1D data which has large scale and plus-minus difference using Python and matplotlib.pyplot

This page shows my suggestion to generate 1D line figure from data which has large scale and plus-minus difference using python and matplotlib.pyplot.

Python Matplotlib Tips: Plot contour figure from data which has large scale and plus-minus difference with minimum limit using Python and matplotlib.pyplot

This page shows my suggestion to generate contour figure from data which has large scale and plus-minus difference using python and matplotlib.pyplot. The minimum value and maximum value can be specified in this code.


In [1]:
import platform
print('python: '+platform.python_version())
import matplotlib.pyplot as plt
from matplotlib import __version__ as matplotlibversion
print('matplotlib: '+matplotlibversion)
import numpy as np
print('numpy: '+np.__version__)
%matplotlib inline
python: 3.6.3
matplotlib: 3.0.1
numpy: 1.15.4

Define two functions to shift linear values to symlog values

In [2]:
def symlog_shift(arr, shift=0):
    # shift array-like to symlog array with shift
    logv = np.abs(arr)*(10.**shift)
    logv[np.where(logv<1.)] = 1.
    logv = np.sign(arr)*np.log10(logv)
    return logv

def symlog_shift_ticks(tks1, tks2, tks3, shift=0):    
    # generate the tick position and the corresponding tick labels in symlog scale with shift
    # tks1, tks2, tks3: tick values in log scale

    # tick positions to show in graph
    tkps = [-v-shift for v in tks1]+tks2+[v+shift for v in tks3]
    # tkck labels in str
    tkls = ['$-10^{%d}$'%(v) for v in tks1]+['']+['$10^{%d}$'%(v) for v in tks3]
    return tkps, tkls
In [3]:
# Shift the minimum value to 1 (log_10{1} = 0) to 0.01 (log_10{0.01} = -2)
SHIFT = 2
x = np.linspace(-100,100,10000)
y = np.sin(x)
logx = symlog_shift(x,shift=SHIFT)
xtkps, xtkls = symlog_shift_ticks([2,1,0,-1],[0],[-1,0,1,2],shift=SHIFT)

Plot the data and write ticks

In [4]:
# plot calculated symlog vlues
plt.plot(logx,y)
plt.xticks(xtkps,xtkls)

# the minimum values for both of plus and minus
plt.text(0,-1,'$-10^{%d}$~$10^{%d}$'%(-SHIFT,-SHIFT),va='bottom',ha='center')

# minimum line
plt.axvline(x=0, ymin=-1.2, ymax=1.2, ls='--', lw=0.5, color='k')

# save
plt.savefig('x-symlog-with-shift.png', dpi=250, bbox_inches='tight', pad_inches=0.02)