【问题标题】:Colorbar for imshow, centered on 0 and with symlog scaleimshow 的颜色条,以 0 为中心,带有符号刻度
【发布时间】:2014-04-17 03:43:38
【问题描述】:

我想生成一个图网格,由几个数组组成,具有正值和负值,具有对数刻度,共享相同的颜色条。

我已经实现了颜色条的共享部分(使用 ImageGrid 和常见的最大值和最小值),并且我知道在只有正值的情况下,我可以在 imshow 调用中使用 LogNorm() 获得对数刻度。但是考虑到负值的存在,我需要一个对称对数刻度的颜色条。

我在 https://stackoverflow.com/a/7741317/1101750 上找到了解决方案,但是运行 Yann 提供的示例代码给了我非常不同的结果,显然是错误的: 查看代码,我无法理解发生了什么。

除此之外,我发现在 Matplotlib 1.2 上,scale.SymmetricalLogScale.SymmetricalLogTransform 要求文档中未解释的新参数(linscale,查看其他转换的代码,我假设将其保留为 1是一个安全值)。

最简单的解决方案是继承 LogNorm 吗?

【问题讨论】:

    标签: numpy matplotlib


    【解决方案1】:

    我过去使用了一个非常简单的方法来做到这一点,而无需进行任何子类化。 matplotlib.colors.SymLogNorm 提供了您需要的大部分功能,除了我发现有必要手动生成刻度线。请注意,此解决方案使用 matplotlib 1.3.0,我可能正在使用 1.2 中没有的功能。

    def imshow_symlog(my_matrix, vmin, vmax, logthresh=5):
        img=imshow( my_matrix ,
                    vmin=float(vmin), vmax=float(vmax),
                    norm=matplotlib.colors.SymLogNorm(10**-logthresh) )
    
        maxlog=int(np.ceil( np.log10(vmax) ))
        minlog=int(np.ceil( np.log10(-vmin) ))
    
        #generate logarithmic ticks 
        tick_locations=([-(10**x) for x in xrange(minlog,-logthresh-1,-1)]
                        +[0.0]
                        +[(10**x) for x in xrange(-logthresh,maxlog+1)] )
    
        cb=colorbar(ticks=tick_locations)
        return img,cb
    

    【讨论】:

      【解决方案2】:

      【讨论】:

        猜你喜欢
        • 2021-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多