【问题标题】:Matplotlib Symmetric Logarithmic colormap not centered at zeroMatplotlib 对称对数颜色图不以零为中心
【发布时间】:2020-12-12 02:26:02
【问题描述】:

我正在研究从matplotlib.colors 导入的默认颜色图SymLogNorm 对于我的数据实际上不是对称的问题。我的数据的平均值和中位数高于零,但我希望零值显示为白色。 SymLogNorm 返回一个中心值略高于零的颜色图——微妙,但绝对引人注目。

有没有办法解决这个问题?

【问题讨论】:

  • 这不是最大的问答,但足以让 +1 抵消 -1。我认为您可以在问题中提供更多详细信息,包括数据和图像,类似于 Chris Willis 的博客。

标签: python matplotlib logarithm colormap


【解决方案1】:

这是我的解决方法 - 它基本上对靠近中心的区域使用手动线性标度,对更远的区域使用对数标度。您指定 lin_thres 和其他参数,就像为 SymLogNorm 指定的一样。

class MidpointLogNorm(colors.SymLogNorm):
    """
    Normalise the colorbar so that diverging bars work there way either side from a prescribed midpoint value)
    e.g. im=ax1.imshow(array, norm=MidpointNormalize(midpoint=0.,vmin=-100, vmax=100))

    All arguments are the same as SymLogNorm, except for midpoint    
    """
    def __init__(self, lin_thres, lin_scale, midpoint=None, vmin=None, vmax=None):
        self.midpoint = midpoint
        self.lin_thres = lin_thres
        self.lin_scale = lin_scale
        #fraction of the cmap that the linear component occupies
        self.linear_proportion = (lin_scale / (lin_scale + 1)) * 0.5
        print(self.linear_proportion)

        colors.SymLogNorm.__init__(self, lin_thres, lin_scale, vmin, vmax)

    def __get_value__(self, v, log_val, clip=None):
        if v < -self.lin_thres or v > self.lin_thres:
            return log_val
        
        x = [-self.lin_thres, self.midpoint, self.lin_thres]
        y = [0.5 - self.linear_proportion, 0.5, 0.5 + self.linear_proportion]
        interpol = np.interp(v, x, y)
        return interpol

    def __call__(self, value, clip=None):
        log_val = colors.SymLogNorm.__call__(self, value)

        out = [0] * len(value)
        for i, v in enumerate(value):
            out[i] = self.__get_value__(v, log_val[i])

        return np.ma.masked_array(out)

我从这里以中点为中心汲取灵感:http://chris35wills.github.io/matplotlib_diverging_colorbar/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-02
    • 1970-01-01
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 2012-10-07
    • 2022-11-25
    • 2013-06-16
    相关资源
    最近更新 更多