Seaborn Heatmap Colorbar Label as Percentage

前端 未结 4 981
醉话见心
醉话见心 2020-12-14 11:31

Given this heat map:

import numpy as np; np.random.seed(0)
import seaborn as sns; sns.set()
uniform_data = np.random.rand(10, 12)
ax = sns.heatmap(uniform_da         


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-14 12:19

    Well, I had a similar problem and figured out how to properly set a formatter. Your example would become something like:

    import numpy as np; np.random.seed(0)
    import seaborn as sns; sns.set()
    
    uniform_data = np.random.rand(10, 12)
    uniform_data = 100 * uniform_data
    
    sns.heatmap(uniform_data,
                cbar_kws={'format': '%.0f%%'})
    

    So, what you have to do is to pass an old-style string formatter to add percentages to colorbar labels. Not exactly what I would name self-evident, but works...

    To show only the first and last, then you add vmax, vmin and an extra parameter to cbar_kws:

    sns.heatmap(uniform_data,
                cbar_kws={'format': '%.0f%%', 'ticks': [0, 100]},
                vmax=100,
                vmin=0)
    

提交回复
热议问题