Seaborn: Wrong ticks on heatmap colorbar when using fmt=.0%

Created on 4 May 2016  路  3Comments  路  Source: mwaskom/seaborn

When using seaborn.heatmap() to plot percentages, the heatmap is correctly annotated to show percentages, i.e., it multiplies the number by 100 and displays in fixed ('f') format, followed by a percent sign. However, the format is not applied to the colorbar ticks, which are shown fixed format.

Example:

import numpy as np; np.random.seed(0)
import seaborn as sns; sns.set()
uniform_data = np.random.rand(10, 12)
sns.heatmap(uniform_data, annot=True, fmt='.0%')

figure_1

Most helpful comment

Maybe I am missing something, but are there cases in which it is useful to have the annotations and the colorbar in different formats? As far as I can tel,l they convey the same information, so showing different formats does not seem to be useful.

Just for compleness, one can achieve the behaviour I expected with some extra code:

from matplotlib.ticker import funcformatter
import numpy as np; np.random.seed(0)
import seaborn as sns; sns.set()
uniform_data = np.random.rand(10, 12)
fmt = lambda x,pos: '{:.0%}'.format(x)
sns.heatmap(uniform_data, annot=true, fmt='.0%',
            cbar_kws={'format': funcformatter(fmt)})

All 3 comments

They're not expected to be; the annotations are just text added to the figure.

Maybe I am missing something, but are there cases in which it is useful to have the annotations and the colorbar in different formats? As far as I can tel,l they convey the same information, so showing different formats does not seem to be useful.

Just for compleness, one can achieve the behaviour I expected with some extra code:

from matplotlib.ticker import funcformatter
import numpy as np; np.random.seed(0)
import seaborn as sns; sns.set()
uniform_data = np.random.rand(10, 12)
fmt = lambda x,pos: '{:.0%}'.format(x)
sns.heatmap(uniform_data, annot=true, fmt='.0%',
            cbar_kws={'format': funcformatter(fmt)})

Maybe I am missing something, but are there cases in which it is useful to have the annotations and the colorbar in different formats? As far as I can tel,l they convey the same information, so showing different formats does not seem to be useful.

Just for compleness, one can achieve the behaviour I expected with some extra code:

from matplotlib.ticker import funcformatter
import numpy as np; np.random.seed(0)
import seaborn as sns; sns.set()
uniform_data = np.random.rand(10, 12)
fmt = lambda x,pos: '{:.0%}'.format(x)
sns.heatmap(uniform_data, annot=true, fmt='.0%',
            cbar_kws={'format': funcformatter(fmt)})

Slight correction to this code to function:

from matplotlib.ticker import FuncFormatter
import numpy as np; np.random.seed(0)
import seaborn as sns; sns.set()
uniform_data = np.random.rand(10, 12)
fmt = lambda x,pos: '{:.0%}'.format(x)
sns.heatmap(directionality, annot=True, fmt='.0%',
            cbar_kws={'format': FuncFormatter(fmt)})
Was this page helpful?
0 / 5 - 0 ratings

Related issues

vinay-jayaram picture vinay-jayaram  路  3Comments

ConstantinoSchillebeeckx picture ConstantinoSchillebeeckx  路  4Comments

sungshine picture sungshine  路  3Comments

wenhaosun picture wenhaosun  路  3Comments

amelio-vazquez-reina picture amelio-vazquez-reina  路  3Comments