如何在 Matplotlib 中的轴上添加 "secondary" 不同字体大小的标签?

How can I add "secondary" labels with different font sizes to axes in Matplotlib?

我正在尝试在 Matplotlib 中生成下图:

用于生成轴的代码(不带标签):

import matplotlib.pyplot as plt
fig,ax = plt.subplots(3,3,sharex=True,sharey=True,
                      constrained_layout=False)

我知道如何添加标签“X 轴标签在这里”和“Y 轴标签在这里”,但我不知道如何放置标签“A”、“B”、“C”、上图中显示的“D”、“E”和“F”。这些标签还应该具有与“此处的 X 轴标签”和“此处的 Y 轴标签”不同的字体大小。有什么建议吗?

一般的方法是使用ax.annotate(),但是对于x-axis,我们可以简单地使用子图标题:

import matplotlib.pyplot as plt
fig, ax = plt.subplots(3,3,sharex=True,sharey=True,
                      constrained_layout=False, figsize=(10, 6))

x_titles = list("DEF")
y_titles = list("ABC")
for curr_title, curr_ax in zip(x_titles, ax[0, :]):
    curr_ax.set_title(curr_title, fontsize=15)
for curr_title, curr_ax in zip(y_titles, ax[:, 0]):
    #the xy coordinates are in % axes from the lower left corner (0,0) to the upper right corner (1,1)
    #the xytext coordinates are offsets in pixel to prevent 
    #that the text moves in relation to the axis when resizing the window
    curr_ax.annotate(curr_title, xy=(0, 0.5), xycoords="axes fraction", 
                     xytext=(-80, 0), textcoords='offset pixels',
                     fontsize=15, rotation="vertical")
plt.show()

示例输出: