Matplotlib: annotate the subtitles in the figure with A, B, C

When sending articles to scientific journals, quite often you have to list the various subtitles of a figure with A, B, ....

enter image description here

This sounds like a very common problem, and I tried to find an elegant way to do this automatically using matplotlib, but I was surprised when I did not find anything. But maybe I'm not using the right search terms. Ideally, I am looking for a way to annotate so that the letters are kept in place relative to the subtitle if the image size has changed or the subtitle has been moved with fig.subplots_adjust, fig.tight_layoutor similar.

Any help or solution would be appreciated.

+4
source share
1

, ax.text.

- :

import numpy as np
import matplotlib.pyplot as plt
import string

fig, axs = plt.subplots(2,2,figsize=(8,8))
axs = axs.flat

for n, ax in enumerate(axs):

    ax.imshow(np.random.randn(10,10), interpolation='none')    
    ax.text(-0.1, 1.1, string.ascii_uppercase[n], transform=ax.transAxes, 
            size=20, weight='bold')

enter image description here

+9

All Articles