Python 任意色条

Python 任意色条,python,matplotlib,colorbar,Python,Matplotlib,Colorbar,我使用imshow()显示的数据在-70,0范围内,我希望使用非线性色条来表示数据,因为我有-70,-60范围和-70,0范围内的模式。 我想用一个任意函数(参见示例)重新缩放/重新规格化颜色条,这样所有的图案都能很好地显示出来 以下是数据和函数的示例: sample_data=(np.ones((20,20))*np.linspace(0,1,20)**3)*70-70 def renorm(value): """ Example of the way I would lik

我使用imshow()显示的数据在-70,0范围内,我希望使用非线性色条来表示数据,因为我有-70,-60范围和-70,0范围内的模式。 我想用一个任意函数(参见示例)重新缩放/重新规格化颜色条,这样所有的图案都能很好地显示出来

以下是数据和函数的示例:

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)

这就是我设法做到的:

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()

您可以更改
gamma
。 但是,不建议使用这种可视化方式,请参见: 在“幂律”->“注”