当调色板发散时,如何使用最小值和最大值设置热图颜色?

How do I set heatmap colors with min and max values when the colorpalette is diverging?

我想设置调色板比例的最大值和最小值。在下面的示例中,我希望调色板的比例从 -10 变为 50,就好像它是一个连续的颜色图一样。我真的不在乎突出显示数字与 "zero line."

的交叉点
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np  
import pandas as pd  
import seaborn as sns

index = np.arange(0, 50)
data = np.random.uniform(low=-10, high=100, size=(50,50))
dft = pd.DataFrame(index=index, columns=index, data=data)
fig, ax = plt.subplots(figsize=(10,10))
cbar_ax = fig.add_axes([.905, 0.125, .05, 0.755])
ax = sns.heatmap(dft, linewidths=.5, cmap=cm.YlGnBu, cbar_kws={'label': 'label'},
                 ax=ax, square=True, cbar_ax=cbar_ax, center=55)
plt.show()

但是如果我这样做:

ax = sns.heatmap(dft, linewidths=.5, cmap=cm.YlGnBu, cbar_kws={'label': 'label'},
                 ax=ax, square=True, cbar_ax=cbar_ax, vmax=50, vmin=-10)

调色板从 -50 变为 50,vmin=-10 被忽略。

来自docs(对于vmin,vmax),

When a diverging dataset is inferred, one of these values may be ignored.

您应该使用 center 参数来指定颜色图居中的值,并结合 vmaxvmin 之一来指定限制。

vmin, vmax = -10, 50
sns.heatmap(..., center=(vmin + vmax) / 2., vmax=vmax)