seaborn heatmap annotation ValueError: Unknown format code 'g' for object of type 'numpy.str_'
--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Droplet of life
--
Chapters
00:00 Seaborn Heatmap Annotation Valueerror: Unknown Format Code 'G' For Object Of Type 'Numpy
01:37 Accepted Answer Score 15
01:59 Thank you
--
Full question
https://stackoverflow.com/questions/6901...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #numpy #matplotlib #seaborn #heatmap
#avk47
    Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Droplet of life
--
Chapters
00:00 Seaborn Heatmap Annotation Valueerror: Unknown Format Code 'G' For Object Of Type 'Numpy
01:37 Accepted Answer Score 15
01:59 Thank you
--
Full question
https://stackoverflow.com/questions/6901...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #numpy #matplotlib #seaborn #heatmap
#avk47
ACCEPTED ANSWER
Score 15
It is a formatting issue. Here  the fmt = '' is required if you are using non-numeric labels (defaults to: fmt='.2g') which consider only for numeric values and throw an error for labels with text format.
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
n1 = 5
n2 = 10
M = np.random.random((n1, n2))
A = np.array([[f'{M[i, j]:.2f}' for j in range(n2)] for i in range(n1)])
A[:, 3] = ''
fig, ax = plt.subplots(figsize = (6, 3))
sns.heatmap(ax = ax, data = M, annot = A, fmt='')
plt.show()