TIF*_*TIF 5 python matplotlib heatmap colorbar seaborn
我有一个带有标签颜色条的seaborn热图,使用:
seaborn.heatmap(df, cbar_kws = {'label': 'Label for color bar axis'})
Run Code Online (Sandbox Code Playgroud)
但颜色条的标签与其刻度标签重叠。有没有办法像普通 matplotlib 绘图的“填充”选项一样,将该标签重新定位到距离刻度线更远的位置(在我的例子中,更靠近右侧)?
不幸的cbar_kws是不接受这个labelpad论点。因此,向标签添加一些填充的一种方法是在绘制颜色条后访问它。
您可以使用ax.collections[0].colorbar来访问该matplotlib.colorbar.Colorbar对象。然后,您可以像平常使用 matplotlib 一样使用 colobar。因此,您可以用来set_label()设置颜色条标签,并且可以使用labelpad=参数:
import seaborn as sns
uniform_data = np.random.rand(10, 12) # random data
ax = sns.heatmap(uniform_data)
cbar = ax.collections[0].colorbar
cbar.set_label('Label for colour bar axis', labelpad=40)
plt.show()
Run Code Online (Sandbox Code Playgroud)