2024-09-27 09:23:41 发布
网友
如何设置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 %'}
不幸的是,seaborn不允许访问它创建的对象。因此,我们需要绕道而行,使用这样一个事实:colorbar是当前图形中的一个轴,它是最后一个创建的轴,因此
ax = sns.heatmap(...) cbar_axes = ax.figure.axes[-1]
对于这个轴,我们可以通过使用ylabel的set_size方法获取ylabel来设置fontsize。
set_size
例如,将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()
请注意,当然也可以通过via
ax = sns.heatmap(data) ax.figure.axes[-1].set_ylabel('Accuracy %', size=20)
不传递关键字参数。
您还可以显式地将axes对象传入heatmap,并直接修改它们:
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)
不幸的是,seaborn不允许访问它创建的对象。因此,我们需要绕道而行,使用这样一个事实:colorbar是当前图形中的一个轴,它是最后一个创建的轴,因此
对于这个轴,我们可以通过使用ylabel的
set_size
方法获取ylabel来设置fontsize。例如,将fontsize设置为20点:
请注意,当然也可以通过via
不传递关键字参数。
您还可以显式地将axes对象传入
heatmap
,并直接修改它们:相关问题 更多 >
编程相关推荐