用于显示的颜色条,以0为中心并具有符号比例 [英] Colorbar for imshow, centered on 0 and with symlog scale

查看:57
本文介绍了用于显示的颜色条,以0为中心并具有符号比例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成一个由多个数组组成的图表网格,它们的正负值为对数刻度,并共享相同的色标.

I want to generate a grid of plots, of several arrays, with positive and negative values, with log scale, sharing the same colorbar.

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

I've achieved the sharing part of the colorbar (using ImageGrid and common max and min values), and I know that I could get a logarithmic scale using LogNorm() on the imshow call in the case of only positive values. But given the presence of negative values, I would need a colorbar on symmetric logarithmic scale.

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

I have found what would be the solution on https://stackoverflow.com/a/7741317/1101750 , but running the sample code Yann provides gives me very different results, cleary wrong: Reviewing the code, I'm not able to grasp what's going on.

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

In addition to that, I've discovered that on Matplotlib 1.2, scale.SymmetricalLogScale.SymmetricalLogTransform asks for a new argument not explained on the documentation (linscale, which looking at the code of other transforms I assume that leaving it as 1 is a safe value).

最简单的解决方案是LogNorm的子类吗?

Is the easiest solution subclassing LogNorm?

推荐答案

过去,我已经使用了一个非常简单的方法来精确地做到这一点,而无需进行任何子类化. matplotlib.colors.SymLogNorm提供了您需要的大多数功能,但我发现必须手动生成刻度线.请注意,此解决方案使用的是matplotlib 1.3.0,而我可能正在使用1.2所不具备的功能.

I've used a pretty simple recipe in the past to do exactly this, without the need to do any subclassing. matplotlib.colors.SymLogNorm provides most of the functionality you need, except that I've found it necessary to generate the tick marks by hand. Note that this solution uses matplotlib 1.3.0, and I may be using features that weren't available with 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

这篇关于用于显示的颜色条,以0为中心并具有符号比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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