具有公平发散色图的不对称色条 [英] Asymmetric Color Bar with Fair Diverging Color Map

查看:51
本文介绍了具有公平发散色图的不对称色条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在散点图中绘制不对称的颜色范围.我希望使用

解决方案

如果我正确理解,那么当前的问题是,以中点为中心的贴图会将颜色均匀从-2缩放为-20(蓝色),类似地(红色)从0到10.

代替缩放 [self.vmin,self.midpoint,self.vmax] = [-2、0、10] ,您应该在[code> [-v_ext,self]之间重新缩放.中点,v_ext] = [-10,0,10] 其中:

  v_ext = np.max([np.abs(self.vmin),np.abs(self.vmax)])## = np.max([2,10]) 

完整的代码如下:

 将numpy导入为np导入matplotlib.pyplot作为plt将matplotlib.colors导入为mcolorsx = np.arange(0,1,1e-1)xlen = x.shape [0]z = np.random.random(xlen ** 2)* 12-2类MidpointNormalize(mcolors.Normalize):def __init __(self,vmin = None,vmax = None,midpoint = None,clip = False):self.midpoint =中点mcolors.Normalize .__ init __(self,vmin,vmax,clip)def __call __(自身,值,剪辑=无):v_ext = np.max([np.abs(self.vmin),np.abs(self.vmax)])x,y = [-v_ext,self.midpoint,v_ext],[0,0.5,1]返回np.ma.masked_array(np.interp(value,x,y))x = np.arange(0,1,1e-1)xlen = x.shape [0]z = np.random.random(xlen ** 2)* 12-2规范= MidpointNormalize(midpoint = 0)splt = plt.scatter(np.repeat(x,xlen),np.tile(x,xlen),c = z,cmap ='地震',s = 400,规范=规范)plt.colorbar(splt)plt.show() 

I'm trying to plot an asymmetric color range in a scatter plot. I want the colors to be a fair representation of the intensity using a diverging color map. I am having trouble changing the color bar to represent this.

For instance, I want to plot x-y data with range [-2, 10] in a scatter plot such that the color bar only shows the range -2 to 10 with neutral color at 0, but the 'intensity' at -2 and 2 are the same.

I've tried using ColorMap Normalization and truncating the color map, but it seems I need some combination of the two that I can't figure out.

MCV Example

x = np.arange( 0, 1, 1e-1 )
xlen = x.shape[ 0 ]
z = np.random.random( xlen**2 )*12 - 2


splt = plt.scatter( 
    np.repeat( x, xlen ), 
    np.tile( x, xlen ), 
    c = z, cmap = 'seismic',
    s = 400
)

plt.colorbar( splt )

By using the MidpointNormalize

class MidpointNormalize(colors.Normalize):
    def __init__(self, vmin=None, vmax=None, midpoint=None, clip=False):
        self.midpoint = midpoint
        colors.Normalize.__init__(self, vmin, vmax, clip)

    def __call__(self, value, clip=None):
        # I'm ignoring masked values and all kinds of edge cases to make a
        # simple example...
        x, y = [self.vmin, self.midpoint, self.vmax], [0, 0.5, 1]
        return np.ma.masked_array(np.interp(value, x, y))

x = np.arange( 0, 1, 1e-1 )
xlen = x.shape[ 0 ]
z = np.random.random( xlen**2 )*12 - 2

norm = MidpointNormalize( midpoint = 0 )

splt = plt.scatter( 
    np.repeat( x, xlen ), 
    np.tile( x, xlen ), 
    c = z, cmap = 'seismic', s = 400,
    norm = norm
)

plt.colorbar( splt )

I can get the colorbar centered at 0, but the intensities are unfair. i.e. The intensity at -2 is much darker that at +2.

The problem I've been having with truncating the color map, is I don't know where the fair place to truncate it is.

Here is an example of the change I want to make in the color bar:

解决方案

If I get you correctly, the issue at hand is that your midpoint-centered map is scaling the color evenly from -2 to 0 (blue) and similarly (red) from 0 to 10.

Instead of scaling [self.vmin, self.midpoint, self.vmax] = [-2, 0, 10], you should rather rescale between [-v_ext, self.midpoint, v_ext] = [-10, 0, 10] where:

v_ext = np.max( [ np.abs(self.vmin), np.abs(self.vmax) ] )  ## = np.max( [ 2, 10 ] )

The complete code could look like:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors

x = np.arange( 0, 1, 1e-1 )
xlen = x.shape[ 0 ]
z = np.random.random( xlen**2 )*12 - 2

class MidpointNormalize(mcolors.Normalize):
    def __init__(self, vmin=None, vmax=None, midpoint=None, clip=False):
        self.midpoint = midpoint
        mcolors.Normalize.__init__(self, vmin, vmax, clip)

    def __call__(self, value, clip=None):
        v_ext = np.max( [ np.abs(self.vmin), np.abs(self.vmax) ] )
        x, y = [-v_ext, self.midpoint, v_ext], [0, 0.5, 1]
        return np.ma.masked_array(np.interp(value, x, y))

x = np.arange( 0, 1, 1e-1 )
xlen = x.shape[ 0 ]
z = np.random.random( xlen**2 )*12 - 2

norm = MidpointNormalize( midpoint = 0 )

splt = plt.scatter( 
    np.repeat( x, xlen ), 
    np.tile( x, xlen ), 
    c = z, cmap = 'seismic', s = 400,
    norm = norm
)

plt.colorbar( splt )
plt.show()

这篇关于具有公平发散色图的不对称色条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆