你可以提供一个虚假的
Colormap
,它应用你所需的标准化,然后将其传递给真正的colormap。以下是这样一个colormap实现:
import matplotlib as mpl
class ReNormColormapAdaptor(mpl.colors.Colormap):
""" Colormap adaptor that uses another Normalize instance
for the colormap than applied to the mappable. """
def __init__(self,base,cmap_norm,orig_norm=None):
if orig_norm is None:
if isinstance(base,mpl.cm.ScalarMappable):
orig_norm = base.norm
base = base.cmap
else:
orig_norm = mpl.colors.Normalize(0,1)
self._base = base
if (
isinstance(cmap_norm,type(mpl.colors.Normalize))
and issubclass(cmap_norm,mpl.colors.Normalize)
):
cmap_norm = cmap_norm(orig_norm.vmin,orig_norm.vmax)
self._cmap_norm = cmap_norm
self._orig_norm = orig_norm
def __call__(self, X, **kwargs):
""" Re-normalise the values before applying the colormap. """
return self._base(self._cmap_norm(self._orig_norm.inverse(X)),**kwargs)
def __getattr__(self,attr):
""" Any other attribute, we simply dispatch to the underlying cmap. """
return getattr(self._base,attr)
由于获取的值已经归一化为[0,1),因此需要知道先前的归一化过程以撤消它(给出orig_norm
)。如果要将色图应用于未归一化的值,请将其留空:
cmap = ReNormColormapAdaptor(mpl.cm.jet,mpl.colors.LogNorm(vmin,vmax))
如果您已经有一个
ScalarMappable
,则可以传递它来代替颜色映射,从中获取颜色映射、先前的归一化和新的归一化限制:
import matplotlib.pyplot as plt
scalar_mappable = plt.imshow(C)
scalar_mappable.set_cmap(ReNormColormapAdaptor(
scalar_mappable,
mpl.colors.LogNorm
))
imshow(data,norm=matplotlib.colors.LogNorm())
不是你要找的吗?它基本上就是你在提示中描述的内容。难道你想对数组进行对数绘图,但是希望颜色条线性绘制吗? - burnpanckLinearSegementedColormap
还是从Colormap
派生的任何其他实例。 - flonk