Seaborn Solar Map Annotation Position in Cells

Annotations in the Seaborn heatmap are, by default, centered in the middle of each cell. Is it possible to move annotations to "upper left".

+5
source share
2 answers

It might be a good idea to use annotations from the annot=True , which are created by annot=True and then shift their width one and a half pixels up and half the width of the pixel. For this offset position to be the upper left corner of the text itself, the arguments ha and va keyword must be set to annot_kws . The shift itself can be performed using translational conversion.

 import seaborn as sns import numpy as np; np.random.seed(0) import matplotlib.pylab as plt import matplotlib.transforms data = np.random.randint(100, size=(5,5)) akws = {"ha": 'left',"va": 'top'} ax = sns.heatmap(data, annot=True, annot_kws=akws) for t in ax.texts: trans = t.get_transform() offs = matplotlib.transforms.ScaledTranslation(-0.48, 0.48, matplotlib.transforms.IdentityTransform()) t.set_transform( offs + trans ) plt.show() 

enter image description here

The behavior is a bit incompatible, as +0.48 in the transformation shifts the label up (against the direction of the axes). This behavior is apparently corrected in the marine version 0.8; for plots on the seashore of 0.8 or higher, use a more intuitive conversion

 offs = matplotlib.transforms.ScaledTranslation(-0.48, -0.48, matplotlib.transforms.IdentityTransform()) 
+4
source

You can use annot_kws marine and set the vertical (va) and horizontal (ha) alignments, as here (sometimes this works poorly):

 ... annot_kws = {"ha": 'left',"va": 'top'} ax = sns.heatmap(data, annot=True, annot_kws=annot_kws) ... 

enter image description here

Another way to place tags manually, as here:

 import seaborn as sns import numpy as np import matplotlib.pylab as plt data = np.random.randint(100, size=(5,5)) ax = sns.heatmap(data) # put labels manually for y in range(data.shape[0]): for x in range(data.shape[1]): plt.text(x, y+1, '%d' % data[data.shape[0] - y - 1, x], ha='left',va='top', color='r') plt.show() 

enter image description here

For more information and understanding the layout of the text (why is the first example working poorly?) In matplotlib read this topic: http://matplotlib.org/users/text_props.html p>

+1
source

Source: https://habr.com/ru/post/1266070/


All Articles