Seaborn热图 - colorbar标签字体大小

时间:2018-02-02 16:17:06

标签: python matplotlib seaborn

如何设置彩条标签的字体大小?

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 %'}

enter image description here

2 个答案:

答案 0 :(得分:5)

不幸的是,seaborn无法访问它创建的对象。因此,需要绕道而行,使用颜色栏是当前图形中的轴并且它是最后创建的轴,因此

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

对于这个轴,我们可以通过使用set_size方法获取ylabel来设置fontsize。

示例,将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()

enter image description here

请注意,当然可以通过

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

没有传递关键字参数。

答案 1 :(得分:0)

您还可以将轴对象显式传递到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)
相关问题