def test_FuncNorm(): def forward(x): return (x**2) def inverse(x): return np.sqrt(x) norm = mcolors.FuncNorm((forward, inverse), vmin=0, vmax=10) expected = np.array([0, 0.25, 1]) input = np.array([0, 5, 10]) assert_array_almost_equal(norm(input), expected) assert_array_almost_equal(norm.inverse(expected), input) def forward(x): return np.log10(x) def inverse(x): return 10**x norm = mcolors.FuncNorm((forward, inverse), vmin=0.1, vmax=10) lognorm = mcolors.LogNorm(vmin=0.1, vmax=10) assert_array_almost_equal(norm([0.2, 5, 10]), lognorm([0.2, 5, 10])) assert_array_almost_equal(norm.inverse([0.2, 5, 10]), lognorm.inverse([0.2, 5, 10]))
def _forward(x): return np.sqrt(x) def _inverse(x): return x**2 N = 100 X, Y = np.mgrid[0:3:complex(0, N), 0:2:complex(0, N)] Z1 = (1 + np.sin(Y * 10.)) * X**2 fig, ax = plt.subplots() norm = colors.FuncNorm((_forward, _inverse), vmin=0, vmax=20) pcm = ax.pcolormesh(X, Y, Z1, norm=norm, cmap='PuBu_r', shading='auto') ax.set_title('FuncNorm(x)') fig.colorbar(pcm, shrink=0.6) plt.show() ############################################################################### # Custom normalization: Manually implement two linear ranges # ---------------------------------------------------------- # # The `.TwoSlopeNorm` described above makes a useful example for # defining your own norm. class MidpointNormalize(colors.Normalize): def __init__(self, vmin=None, vmax=None, vcenter=None, clip=False):