Skip to content

Instantly share code, notes, and snippets.

@sccu
Last active January 22, 2019 00:40
Show Gist options
  • Save sccu/4d281e8b09a508902d48eba9bbb6f657 to your computer and use it in GitHub Desktop.
Save sccu/4d281e8b09a508902d48eba9bbb6f657 to your computer and use it in GitHub Desktop.
Unsaturated scale function in python
from matplotlib import pyplot
import numpy as np
def scale(x, k=1.0, func="log"):
k = max(1e-9, abs(k))
if func == "log":
return np.sign(x) * np.log(k*np.abs(x)+1) / k
elif func == "sqrt":
return np.sign(x) * (np.sqrt(k*np.abs(x)+1) * 2 - 2) / k
else:
raise ValueError(f"Invalid func name: {func}")
x = [i for i in range(-100, 100)]
pyplot.plot(x, scale(x))
pyplot.grid(b=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment