Python Forum

Full Version: How to manually define color bar scale in seaborn heatmap
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I want to define color bar, how to define manually.
I use below example:

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_data)
The automatic color bar ticks getting here are 0.2,0.4,0.6,0.8, but I want to display 0.0,0.2,0.4,0.6,0.8,1.0. How to make this.
Try the following example:

import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
uniform_data = np.random.rand(10, 12)
ax = sns.heatmap(uniform_data, cbar_kws={'ticks': [0.0, 0.2, 0.4, 0.5, 0.7, 0.8, 1.0]}, vmin=0, vmax=1) 
plt.show()
Thanks, it works.
Hi Rajesh,

When we create seaborn heatmap then automatically color bar also generate. Depend on creator requirement, we can modify it. You want to display ticks, so use cbar_kws sns.heatmap() parameter and pass keys(ticks) and values mapping for change the style and format of seaborn heatmap color bar

import numpy as np; np.random.seed(0)
import seaborn as sns; sns.set()

uniform_data = np.random.rand(10, 12)

# color bar keyword arguments
cbar_kws = {"shrink":1,
            'extend':'min', 
            'extendfrac':.1, 
            "drawedges":True, 
            'ticks': [0.0, 0.2, 0.4, 0.5, 0.7, 0.8, 1.0], # set ticks of color bar
            'label':'Color Bar'}

ax = sns.heatmap(uniform_data, cbar_kws=cbar_kws, vmin=0, vmax=1)