更改 Seaborn 热图颜色条的高度

Change the height of a Seaborn heatmap colorbar

我有以下 Python 代码来使用 Seaborn 包创建热图:

f, ax = plt.subplots(figsize=(21,5))
heatmap = sns.heatmap(significantBig5[basic].as_matrix().T,mask=labelsDf.T,
     annot=False,fmt='.2f',yticklabels=basic_labels,linewidths=0.5,square=True,
     xticklabels=traits)
plt.xticks(rotation=70)

这将创建以下热图:

我想格式化此热图,使右侧的颜色栏更短。有办法吗?

使用cbar_ax参数并设置您喜欢的颜色条的位置:

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

arr = np.random.random((1,9))
df = pd.DataFrame(arr)
fig, ax = plt.subplots(1, 1)
cbar_ax = fig.add_axes([.905, .3, .05, .3])
sns.heatmap(arr, ax=ax, cbar_ax = cbar_ax, cbar=True)

plt.show()

您还可以为 sns 热图颜色条使用额外的关键字参数 cbar_kws。他们修改了 matplotlib colorbar 记录的设置 here

使用@Serenity 的代码和 shrinkage 关键字我们得到:

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

arr = np.random.random((1,9))
df = pd.DataFrame(arr)
sns.heatmap(arr,cbar=True,cbar_kws={"shrink": .82})

plt.show()