6

My question is something related to: How to add title to Seaborn Heatmap color bar?

I have the following code:

import seaborn as sns
import pandas as pd
data = pd.DataFrame({'x':(1,2,3,4),'y':(1,2,3,4),'z':(1400,1500,2300,2000)})

sns.heatmap(data.pivot_table(index='y', columns='x', values='z'), 
                             cbar_kws={'label': 'colorbar title'})

enter image description here

And I would like to add thousands separator to the color bar thickers, 2'250 or 2,250 for example. How can I accomplish this?

EDIT: I was redirected here: Formatting Numbers to have a comma on plt.colorbar() in Python Indeed it makes sense, but, how do I extract that from the seaborn heatmap?

EDIT 2 - Solution:

import seaborn as sns
import pandas as pd
from matplotlib.ticker import FuncFormatter

data = pd.DataFrame({'x':(1,2,3,4),'y':(1,2,3,4),'z':(1400,1500,2300,2000)})

comma_fmt = FuncFormatter(lambda x, p: format(int(x), ','))

sns.heatmap(data.pivot_table(index='y', columns='x', values='z'), 
                             cbar_kws={'label': 'colorbar title','format':comma_fmt}).get_figure()
jvaldiviezo
  • 101
  • 9
  • How could I extract the seaborn object to modify it? I was looking at docs but there is no clear way to do it – jvaldiviezo Jan 22 '18 at 16:20
  • Sorry, since you already managed to get the title into the colorbar, I wouldn't have thought that it was a problem to implement the linked solution. It's simply `cbar_kws={'label': 'colorbar title', "format": comma_fmt}`, where `comma_fmt` is the one from the linked question. Next time, also use the @ notification to the people who closed the question if you want to ask further details. – ImportanceOfBeingErnest Jan 22 '18 at 18:51
  • @ImportanceOfBeingErnest Thanks! I've updated the code for reference with the solution. – jvaldiviezo Jan 22 '18 at 22:43

0 Answers0