反转LogNorm以正确显示热图的{color}标签

时间:2017-02-22 19:27:45

标签: python matplotlib heatmap colorbar

我使用(以下版本)以下代码生成具有相邻颜色条的热图:

# imports
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from mpl_toolkits.axes_grid1 import make_axes_locatable

# create some dummy-data
matrix = np.array([[1, 2, 3],[2, 1, 3], [3, 1, 2]])
# scale the data
scaled = matrix / matrix.sum(axis=1).reshape(-1,1)

这就是scaled看起来的样子(缩放在这个例子中没有区别,但是在缩放数据用于分层链接的预期用例中):

array([[ 0.16666667,  0.33333333,  0.5       ],
   [ 0.33333333,  0.16666667,  0.5       ],
   [ 0.5       ,  0.16666667,  0.33333333]])

现在我创建情节(注意使用LogNorm):

_, ax_heatmap = plt.subplots()
heatmap = ax_heatmap.pcolor(
    scaled, edgecolors='w',
    cmap=mpl.cm.viridis_r,
    norm=mpl.colors.LogNorm())
ax_heatmap.autoscale(tight=True)
ax_heatmap.set_aspect('equal')
divider_h = make_axes_locatable(ax_heatmap)
cax = divider_h.append_axes("right", "3%", pad="1%")
plt.colorbar(heatmap, cax=cax, ticks=np.unique(scaled))
cax.yaxis.set_major_formatter(
        mpl.ticker.FuncFormatter(
            lambda y, pos: ('{:.1f}'.format(y))))
plt.tight_layout()
plt.show()

enter image description here

结果图符合预期,但颜色条上的刻度标签与预期值不对应,预期值应与scaled中的值对应。我知道提供给FuncFormatter的函数应该用来解决这个问题,但是它不清楚它应该反转哪种转换组合(或者它是否使用LogNorm是不合适的。)

1 个答案:

答案 0 :(得分:2)

刚刚找到解决方案。似乎LogNorm有一个反向方法。首先使用正确的vmin和vmax初始化LogNorm对象,可以将其反转提供给FuncFormatter

_, ax_heatmap = plt.subplots()
norm = mpl.colors.LogNorm(vmin=scaled.min(), vmax=scaled.max())
heatmap = ax_heatmap.pcolor(
    scaled, edgecolors='w',
    cmap=mpl.cm.viridis_r,
    norm=norm)
ax_heatmap.autoscale(tight=True)
ax_heatmap.set_aspect('equal')
divider_h = make_axes_locatable(ax_heatmap)
cax = divider_h.append_axes("right", "3%", pad="1%")
plt.colorbar(heatmap, cax=cax, ticks=np.unique(scaled))
cax.yaxis.set_major_formatter(
        mpl.ticker.FuncFormatter(
            lambda y, pos: ('{:.5f}'.format(norm.inverse(y)))))
plt.tight_layout()
plt.show()

enter image description here