Seaborn热图-彩色条标签字体大小

2024-09-27 09:23:41 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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


Tags: table标签axcmap字体大小snsheatmapvmax
2条回答

不幸的是,seaborn不允许访问它创建的对象。因此,我们需要绕道而行,使用这样一个事实:colorbar是当前图形中的一个轴,它是最后一个创建的轴,因此

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

对于这个轴,我们可以通过使用ylabel的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

请注意,当然也可以通过via

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

不传递关键字参数。

您还可以显式地将axes对象传入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)

相关问题 更多 >

    热门问题