简体   繁体   English

Seaborn热图 - colorbar标签字体大小

[英]Seaborn heatmap - colorbar label font size

How do I set the font size of the colorbar label? 如何设置彩条标签的字体大小?

ax=sns.heatmap(table, vmin=60, vmax=100, xticklabels=[4,8,16,32,64,128],yticklabels=[2,4,6,8], cmap="PuBu",linewidths=.0, 
        annot=True,cbar_kws={'label': 'Accuracy %'}

在此输入图像描述

Unfortunately seaborn does not give access to the objects it creates. 不幸的是,seaborn无法访问它创建的对象。 So one needs to take the detour, using the fact that the colorbar is an axes in the current figure and that it is the last one created, hence 因此,需要绕道而行,使用颜色条是当前图形中的轴并且它是最后创建的轴,因此

ax = sns.heatmap(...)
cbar_axes = ax.figure.axes[-1]

For this axes, we may set the fontsize by getting the ylabel using its set_size method. 对于这个轴,我们可以通过使用其set_size方法获取ylabel来设置fontsize。

Example, setting the fontsize to 20 points: 例如,将fontsize设置为20个点:

import matplotlib.pyplot as plt
import numpy as np; np.random.seed(0)
import seaborn as sns
data = np.random.rand(10, 12)*100
ax = sns.heatmap(data, cbar_kws={'label': 'Accuracy %'})
ax.figure.axes[-1].yaxis.label.set_size(20)

plt.show()

在此输入图像描述

Note that the same can of course be achieved by via 注意,当然可以通过via实现相同的目的

ax = sns.heatmap(data)
ax.figure.axes[-1].set_ylabel('Accuracy %', size=20)

without the keyword argument passing. 没有关键字参数传递。

You could also explicitly pass in the axes objects into heatmap and modify them directly: 您还可以将轴对象显式传入heatmap并直接修改它们:

grid_spec = {"width_ratios": (.9, .05)}
f, (ax, cbar_ax) = plt.subplots(1,2, gridspec_kw=grid_spec) 
sns.heatmap(data, ax=ax, cbar_ax=cbar_ax, cbar_kws={'label': 'Accuracy %'})
cbar_ax.yaxis.label.set_size(20)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM