When submitting papers to scientific journals one quite frequently needs to enumerate the different subplots of a figure with A, B, ... .
This sounds like a very common problem and I was trying to find an elegant way to do that automatically with matplotlib, but I was surprised to find nothing on it. But maybe I am not using the right search terms. Ideally, I am searching for a way to annotate such that the letters stay in place relative to the subplot if the figure is resized or the subplot is moved via fig.subplots_adjust
, fig.tight_layout
, or similar.
Any help or solution will appreciated.
To create multiple plots use matplotlib. pyplot. subplots method which returns the figure along with Axes object or array of Axes object. nrows, ncols attributes of subplots() method determine the number of rows and columns of the subplot grid.
The subplot() Function #the figure has 1 row, 2 columns, and this plot is the first plot. plt.subplot(1, 2, 2) #the figure has 1 row, 2 columns, and this plot is the second plot.
If you want the annotation relative to the subplot then plotting it using ax.text
seems the most convenient way to me.
Consider something like:
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With