反转 LogNorm 以正确显示热图的 tick_labels 颜色条

Invert LogNorm for correct display of tick_labels of colorbar for heatmap

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

# 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()

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

刚刚找到解决方案。看来 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()