枚举matplotlib图中的图

Se *_*orm 6 enumerate matplotlib title

在matplotlib图中,我想用a),b),c)等列举所有(子)图.有没有办法自动执行此操作?

到目前为止,我使用了各个图表的标题,但这远非理想,因为我希望数字保持对齐,而可选的实际标题应该以图形为中心.

tac*_*ell 7

import string
from itertools import cycle
from six.moves import zip

def label_axes(fig, labels=None, loc=None, **kwargs):
    """
    Walks through axes and labels each.

    kwargs are collected and passed to `annotate`

    Parameters
    ----------
    fig : Figure
         Figure object to work on

    labels : iterable or None
        iterable of strings to use to label the axes.
        If None, lower case letters are used.

    loc : len=2 tuple of floats
        Where to put the label in axes-fraction units
    """
    if labels is None:
        labels = string.lowercase

    # re-use labels rather than stop labeling
    labels = cycle(labels)
    if loc is None:
        loc = (.9, .9)
    for ax, lab in zip(fig.axes, labels):
        ax.annotate(lab, xy=loc,
                    xycoords='axes fraction',
                    **kwargs)
Run Code Online (Sandbox Code Playgroud)

示例用法:

from matplotlib import pyplot as plt
fig, ax_lst = plt.subplots(3, 3)
label_axes(fig, ha='right')
plt.draw()

fig, ax_lst = plt.subplots(3, 3)
label_axes(fig, ha='left')
plt.draw()
Run Code Online (Sandbox Code Playgroud)

这对我来说似乎很有用,我把它放在一个要点:https://gist.github.com/tacaswell/9643166