transform colors in colorbar, not the ticks

When using a custom normalization, for instance PowerNorm, we can adjust the mapping between values and the colors. If we then show a corresponding colorbar, we can see the change when observing the ticks (compare left and right plot in the following picture).

Is there a way to use the normalization like on the left, but then have a colorbar where the colours are "squished" to one end, but the ticks remain equidistant (like on the right side)?

enter image description here

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import PowerNorm
x, _ = np.meshgrid(*2*(np.linspace(-1, 1, 100),))
# with normalization: transformation is applied to image as desired, but the ticks at the colorbar are not equidistant
plt.imshow(x, norm=PowerNorm(gamma=4, vmin=-1, vmax=1))
# without normalization

>Solution :

It looks like you need to set the y axis of the color bar to be linear:

cb = plt.colorbar()'linear') 

enter image description here

Leave a Reply