I am searching for an elegant solution to placing a figure label (A, B, C) above the corner of each subplot using matplotlib- but have each be the exact same distance from the subplot axes corner. The problem I am having is that typical labeling solutions take advantage of the axes fraction- so it is easy to place A,B,C in the same place relative to each plot.
import matplotlib as mpl
import matplotlib.pyplot as plt
fig, ax = plt.subplots(2,2, figsize = (10,10))
texts = ['A', 'B', 'C', 'D']
axes = fig.get_axes()
for a,l in zip(axes, texts):
a.annotate(l, xy=(-0.1, 1.1), xycoords="axes fraction", fontsize=15, weight = 'bold')
plt.suptitle('Label_Distance Consistent', fontsize = 20)
however, if the plots are different sizes you will get labels that are variably far from the corners of the plots (due to aspect ratio). See Label A and C for example. I am looking for a good way to ensure proportional distance of labels from axes corners for panels containing multiple sizes/aspect ratio subplots, and/or to explicitly set text a specific distance (in inches or maybe figure coordinate units) from axes corners.
In the past I have placed same sized square axes at the corner of each subplot axes in the panel, made those invisible, and scaled text to these, but it is a convoluted approach.
fig, ax = plt.subplots(2,2, figsize = (10,10))
ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3)
ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2)
ax3 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)
ax4 = plt.subplot2grid((3, 3), (2, 0))
ax5 = plt.subplot2grid((3, 3), (2, 1))
axes = fig.get_axes()
texts = ['A', 'B', 'C', 'D', 'E']
for a,l in zip(axes, texts):
a.annotate(l, xy=(-0.1, 1.1), xycoords="axes fraction", fontsize=15, weight = 'bold')
plt.suptitle('Label Distance Inconsistent', fontsize = 20)