Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib: Annotate Subplots in a Figure with A, B, C [duplicate]

Tags:

When submitting papers to scientific journals one quite frequently needs to enumerate the different subplots of a figure with A, B, ... .

enter image description here

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.

like image 685
fabee Avatar asked Aug 28 '14 08:08

fabee


People also ask

How do I create a double subplot in Matplotlib?

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.

What does PLT subplot 1 2 2 mean?

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.


1 Answers

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') 

enter image description here

like image 121
Rutger Kassies Avatar answered Sep 20 '22 05:09

Rutger Kassies