【问题标题】:Seaborn heatmap - colorbar label font sizeSeaborn 热图 - 颜色条标签字体大小
【发布时间】:2018-02-02 16:17:05
【问题描述】:

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

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

【问题讨论】:

    标签: python matplotlib seaborn


    【解决方案1】:

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

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

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

    例如,设置字体大小为 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)
    

    没有传递关键字参数。

    【讨论】:

    • 谢谢,太好了。注释格式呢?你知道怎么改吗?
    • 从the documentation 可以看出这一点。
    【解决方案2】:

    您也可以将坐标区对象显式传入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)
    

    【讨论】:

      猜你喜欢
      • 2016-04-21
      • 2016-09-11
      • 2015-03-06
      • 2020-12-02
      • 1970-01-01
      • 1970-01-01
      • 2018-05-07
      • 1970-01-01
      • 2016-02-20
      相关资源
      最近更新 更多