1

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.

user1993416
  • 698
  • 1
  • 9
  • 28
  • Try changing DPI when generating a figure. It happens that in Python preview everything is ok, and you see that the font is too small only after you will save the figure to a (PDF/PNG) file. Bear in mind that changing DPI will influence all fonts, not only tick labels in 16x16 matrices. For the latter you need to access axes settings of the 16x16 subplots. The general procedure for that is: generate fig and axes (the subplots), plot your data, and then modify particular subplot's axes before displaying/saving the plot. – Mikolaj Buchwald Mar 04 '22 at 11:02
  • @MikolajBuchwald I have changed the dpi to 500 and the saved plot shows the same font size of 1000 dpi. – user1993416 Mar 04 '22 at 11:34
  • Then only setting font size per axes' subplot will help you. I will post and answer below. – Mikolaj Buchwald Mar 04 '22 at 13:21

2 Answers2

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/

Mikolaj Buchwald
  • 160
  • 4
  • 14
1

adjust 45 to the angle you want, and adjust the fontsize to the desired value:

plt.xticks(rotation=45, ha='right', fontsize=32) 
amy989
  • 313
  • 1
  • 7