Sebastian
Sebastian

Reputation: 113

Round decimal places seaborn heatmap labels

I have a heat map that gets its labels automatically, however some of them are long decimals (I don't understand why because the values I used for the heatmap have 3 decimals, for example 0.009 appears as a long decimal), I would like to round all labels to 4 decimals places. Here is the relevant part of the code:

table1 = df.pivot('ToxinK','Khill','Fraction') #Putting information from the dataframe into a pivot table
sns.heatmap(table1,cmap='Purples',norm=LogNorm())

enter image description here

I have tried a solution that I found on seaborn too long numeric labels, however is not working for me, I don't know why Here is the attempt:

majorFormatter = FormatStrFormatter('%0.4f')
ax1=sns.heatmap(table1,cmap='Purples',norm=LogNorm())
ax1.yaxis.set_major_formatter(majorFormatter)

enter image description here

Any advice or piece of information on how to solve this problem would be incredibly useful. Thanks in advance!

Upvotes: 9

Views: 17045

Answers (1)

To format the annotations, enter the argument fmt='.4f' for 4 decimal places. Taking your first code as example:

sns.heatmap(table1, cmap='Purples', fmt='.4f')

Upvotes: 5

Related Questions