'How to change the font labels of heatmap

I would like to use the following heatmap generated with Python:

import seaborn as sn
import pandas as pd
import matplotlib.pyplot as plt
# CMG
array = [[129,  10,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
         [ 15,  97,  12,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
         [  0,  14,  74,  17,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
         [  0,   0,  24,  74,  25,   1,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
         [  0,   0,   2,  28,  87,   8,   9,   1,   0,   0,   0,   0,   0,   0,   0,   0],
         [  0,   0,   0,   4,  17,  91,  13,   1,   1,   0,   0,   0,   0,   0,   0,   0],
         [  0,   0,   0,   2,   8,  16,  45,  25,  15,   2,   0,   0,   0,   0,   0,   0],
         [  0,   0,   0,   0,   0,  10,  27,  49,  43,   9,   0,   0,   0,   0,   0,   0],
         [  0,   0,   0,   0,   1,   1,   9,  24,  77,  10,   2,   0,   0,   0,   0,   0],
         [  0,   0,   0,   0,   0,   0,   2,   6,  26,  58,  26,   4,   0,   0,   0,   0],
         [  0,   0,   0,   0,   0,   0,   0,   2,   4,  37,  70,  14,   1,   0,   0,   0],
         [  0,   0,   0,   0,   0,   0,   0,   0,   0,   1,  10,  63,  36,   4,   0,   0],
         [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   6,  31,  67,  25,   4,   6],
         [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   1,  18,  41,  25,  10,  19],
         [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   2,  34,  29,  13,  42],
         [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   1,  17,  14,   5,  97]]
df_cm = pd.DataFrame(array, index = [i for i in [r'$s_1$',r'$s_2$',r'$s_3$',r'$s_4$',r'$s_5$',r'$s_6$',r'$s_7$',r'$s_8$',r'$s_9$',r'$s_{10}$',r'$s_{11}$',r'$s_{12}$',r'$s_{13}$',r'$s_{14}$',r'$s_{15}$',r'$s_{16}$']],
                  columns = [i for i in [r'$s_1$',r'$s_2$',r'$s_3$',r'$s_4$',r'$s_5$',r'$s_6$',r'$s_7$',r'$s_8$',r'$s_9$',r'$s_{10}$',r'$s_{11}$',r'$s_{12}$',r'$s_{13}$',r'$s_{14}$',r'$s_{15}$',r'$s_{16}$']])
plt.figure(figsize = (17,14))
#plt.figure()
sn.set(font_scale=2.15)
ax=sn.heatmap(df_cm, 
           annot=False,
           cmap="OrRd",
           annot_kws={"size": 16},cbar=True,
           linewidths=0.5, linecolor='white')
ax.set_aspect(1) # pone la matrix cuadrada
plt.yticks(rotation=0)
ax.xaxis.tick_top() # x axis on top
ax.xaxis.set_label_position('top')
ax.tick_params(length=0)
# plt.title('Confusion Matrix')
plt.savefig('ConfusionCMG.pdf', format='pdf', dpi=1000, bbox_inches='tight')
plt.show()

In a two columns page in a latex multiplot like this:

enter image description here

The labels in the 8x8 matrices are clear, but not those of the 16x16 that look too small.

I have tried to change the size in the Python code, but in the pdf file, the label is too small. I would like to know how to make the two-columns page's labels clearer. Make font bigger, or rotate the top labels and make them bigger.



Solution 1:[1]

You can set font size (and rotation) of the tick labels on the axes -- for particular subplots.

The general way to do that is:

ticks_font_size = 5
rotation = 90

ax.set_yticklabels(ax.get_yticklabels(), size=ticks_font_size)
ax.set_xticklabels(ax.get_xticklabels(), rotation=rotation, size=ticks_font_size)

(For your example probably you will have to create/generate the figure and the axes first.)

Here is my example of setting the tick label font size for axis (see a function in In [21]): https://gitlab.com/mikbuch/lincalc/-/blob/master/ipython_notebooks/Generate_laterality_indices.ipynb

See also this, simpler example: https://www.delftstack.com/howto/matplotlib/how-to-set-tick-labels-font-size-in-matplotlib/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mikolaj Buchwald