Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 594
jadsq Avatar asked Jul 25 '26 15:07

jadsq


1 Answers

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"

like image 87
Ophir Carmi Avatar answered Jul 27 '26 04:07

Ophir Carmi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!