Python Forum
How to manually define color bar scale in seaborn heatmap
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to manually define color bar scale in seaborn heatmap
#1
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.
Reply
#2
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()
Reply
#3
Thanks, it works.
Reply
#4
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  manually input data jpatierno 0 316 Nov-10-2023, 02:32 AM
Last Post: jpatierno
  Turtle Star Fill Color Yellow-White Interchanging Color Effect codelab 9 902 Oct-25-2023, 09:09 AM
Last Post: codelab
  Trying to loop through code to plot seaborn line plots across multiple subplots eyavuz21 0 1,575 Dec-05-2022, 10:46 AM
Last Post: eyavuz21
  How to properly scale text in postscript conversion to pdf? philipbergwerf 3 1,084 Nov-07-2022, 01:30 PM
Last Post: philipbergwerf
  How to plot seaborn heatmap on top of a background circle SriRajesh 0 1,368 Jul-09-2022, 04:00 AM
Last Post: SriRajesh
  Installing and Using Seaborn Led_Zeppelin 4 2,471 Jun-07-2022, 09:44 PM
Last Post: snippsat
  KeyError: 0 when trying to dedupe and match records at scale Catalytic 1 2,109 Apr-07-2022, 06:34 PM
Last Post: deanhystad
  How to increase the size of a png picture for the heatmap of the correlation? lulu43366 9 3,386 Oct-06-2021, 04:15 PM
Last Post: deanhystad
  How to remove a column or two columns in a correlation heatmap? lulu43366 3 5,081 Sep-30-2021, 03:47 PM
Last Post: lulu43366
  Matplotlib scale julienhofmann 0 1,782 Apr-04-2021, 08:50 AM
Last Post: julienhofmann

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020