jadsq
jadsq

Reputation: 3382

Arbitrary colorbar

I have data that is in the range -70,0 that I display using imshow() and would like to use a non-linear colorbar to represent the data as I have paterns both in the -70,-60 range and -70,0 range. I would like the easiest way to rescale/renormalize using an arbitrary function (see example) the colorbar so that all paterns appear nicely.

Here is an example of data and function:

sample_data=(np.ones((20,20))*np.linspace(0,1,20)**3)*70-70

def renorm(value):
    """
    Example of the way I would like to adjust the colorbar but it might as well be an arbitrary function
    Returns a number between 0 and 1 that would correspond to the color wanted on the original colorbar
    For the cmap 'inferno' 0 would be the dark purple, 0.5 the purplish orange and 1 the light yellow
    """

    return np.log(value+70+1)/np.log(70+1)

expected colorbar

Upvotes: 2

Views: 756

Answers (1)

Ophir Carmi
Ophir Carmi

Reputation: 2921

This is what I managed to do:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import PowerNorm
sample_data=(np.ones((20,20))*np.linspace(0,1,20)**3)*70-70
plt.figure()
im = plt.imshow(sample_data+70, norm=PowerNorm(gamma=0.5))
cbar = plt.colorbar(orientation='horizontal')
cbar.ax.set_xticklabels(np.arange(-70, 0, 8))
plt.show()

enter image description here

You can change the gamma. However, this kind of visualization is not recommended, see: http://matplotlib.org/users/colormapnorms.html under "Power-law" -> "Note"

Upvotes: 2

Related Questions