Matplotlib:用 A、B、C 注释图中的子图 [英] Matplotlib: Annotate Subplots in a Figure with A, B, C
问题描述
当向科学期刊提交论文时,人们经常需要用 A、B、... 枚举一个图的不同子图.
When submitting papers to scientific journals one quite frequently needs to enumerate the different subplots of a figure with A, B, ... .
这听起来像是一个非常普遍的问题,我试图找到一种优雅的方式来使用 matplotlib 自动执行此操作,但我很惊讶地发现它没有任何内容.但也许我没有使用正确的搜索词.理想情况下,我正在寻找一种注释方法,以便在调整图形大小或通过 fig.subplots_adjust
、fig.tight_layout<移动子图时,字母相对于子图保持原位/code>,或类似的.
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.
推荐答案
如果你想要相对于子图的注释,那么使用 ax.text
绘制它对我来说似乎是最方便的方法.
If you want the annotation relative to the subplot then plotting it using ax.text
seems the most convenient way to me.
考虑如下:
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')
这篇关于Matplotlib:用 A、B、C 注释图中的子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!