如何更改热图的字体标签
How to change the font labels of heatmap
我想使用由 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()
在像这样的乳胶多图的两列页面中:
8x8的矩阵标签清晰,16x16的标签不清晰,看起来太小了。
我曾尝试在 Python 代码中更改大小,但在 pdf 文件中,标签太小了。我想知道如何使两栏页面的标签更清晰。将字体调大,或旋转顶部标签使其变大
您可以为特定的子图设置轴上刻度标签的字体大小(和旋转)。
一般的做法是:
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)
(对于您的示例,您可能必须先 create/generate 图形和坐标轴。)
这里是我设置坐标轴刻度标签字体大小的例子(见In [21]
中的一个函数):https://gitlab.com/mikbuch/lincalc/-/blob/master/ipython_notebooks/Generate_laterality_indices.ipynb
另见这个更简单的例子:https://www.delftstack.com/howto/matplotlib/how-to-set-tick-labels-font-size-in-matplotlib/
调整45到你想要的角度,调整fontsize到你想要的值:
plt.xticks(rotation=45, ha='right', fontsize=32)
我想使用由 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()
在像这样的乳胶多图的两列页面中:
8x8的矩阵标签清晰,16x16的标签不清晰,看起来太小了。
我曾尝试在 Python 代码中更改大小,但在 pdf 文件中,标签太小了。我想知道如何使两栏页面的标签更清晰。将字体调大,或旋转顶部标签使其变大
您可以为特定的子图设置轴上刻度标签的字体大小(和旋转)。
一般的做法是:
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)
(对于您的示例,您可能必须先 create/generate 图形和坐标轴。)
这里是我设置坐标轴刻度标签字体大小的例子(见In [21]
中的一个函数):https://gitlab.com/mikbuch/lincalc/-/blob/master/ipython_notebooks/Generate_laterality_indices.ipynb
另见这个更简单的例子:https://www.delftstack.com/howto/matplotlib/how-to-set-tick-labels-font-size-in-matplotlib/
调整45到你想要的角度,调整fontsize到你想要的值:
plt.xticks(rotation=45, ha='right', fontsize=32)