更改Seaborn热图颜色条的高度

sea*_*314 8 python matplotlib heatmap colorbar seaborn

我有以下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)
Run Code Online (Sandbox Code Playgroud)

这将创建以下热图: 样本热图

我想格式化此热图,以便右侧的颜色条更短.有没有办法做到这一点?

Ree*_*rds 7

您还可以使用cns热图颜色栏cbar_kws的额外关键字参数.他们修改了此处记录的matplotlib颜色条的设置

使用@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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Ser*_*ity 6

使用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()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明