镜像 Seaborn 热图的色标,包括颜色和标签

Mirror the color scale of a Seaborn heatmap, both colors and labels

我有一个使用 Seaborn 模块生成的热图,如图 here。

由于 table 中的值通常随着与 MSL 的距离增加而增加(table 中的值向下增加),我想镜像色标,使深蓝色最下面是对应的标签(12),浅黄色是最上面的,是标签(3)。

我只找到了一种反转颜色的方法,但标签仍然保留在原位。我怎样才能反映整个规模(包括颜色和标签)?

谢谢!

invert_yaxis() 在颜色条的轴上反转刻度标签和颜色条(invert_xaxis() 对水平颜色条执行相同操作)。

Seaborn 的热图不直接 return 生成的颜色条的句柄。可以通过剧情的ax获得:cbar = ax.collections[0].colorbar.

这是一个例子:

import seaborn as sns
import numpy as np
import matplotlib.pylab as plt

x, y = np.meshgrid(np.arange(20), np.arange(20))
arr = (x * y) % 10
ax = sns.heatmap(arr, annot=True, cbar=True)
cbar = ax.collections[0].colorbar
cbar.ax.invert_yaxis()
plt.show()