Enumerate plots in matplotlib figure

前端 未结 3 884
孤独总比滥情好
孤独总比滥情好 2020-12-20 14:36

In a matplotlib figure I would like to enumerate all (sub)plots with a), b), c) and so on. Is there a way to do this automatically?

So far I use the individual plots

相关标签:
3条回答
  • 2020-12-20 15:15

    A super quick way to do this is to take advantage of the fact that chr() casts integers to characters. Since a-z fall in the range 97-122, one can do the following:

    import matplotlib.pyplot as plt
    
    fig,axs = plt.subplots(2,2)
    for i,ax in enumerate(axs.flat, start=97):
      ax.plot([0,1],[0,1])
      ax.text(0.05,0.9,chr(i)+')', transform=ax.transAxes)
    

    which produces:

    0 讨论(0)
  • 2020-12-20 15:16

    I wrote a function to do this automatically, where the label is introduced as a legend:

    import numpy
    import matplotlib.pyplot as plt
    
    def setlabel(ax, label, loc=2, borderpad=0.6, **kwargs):
        legend = ax.get_legend()
        if legend:
            ax.add_artist(legend)
        line, = ax.plot(numpy.NaN,numpy.NaN,color='none',label=label)
        label_legend = ax.legend(handles=[line],loc=loc,handlelength=0,handleheight=0,handletextpad=0,borderaxespad=0,borderpad=borderpad,frameon=False,**kwargs)
        label_legend.remove()
        ax.add_artist(label_legend)
        line.remove()
    
    fig,ax = plt.subplots()
    ax.plot([1,2],[1,2])
    setlabel(ax, '(a)')
    plt.show()
    

    The location of the label can be controlled with loc argument, the distance to the axis can be controlled with borderpad argument (negative value pushes the label to be outside the figure), and other options available to legend also can be used, such as fontsize. The above script gives such figure:

    0 讨论(0)
  • 2020-12-20 15:22
    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.ascii_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)
    

    example usage:

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

    This seems useful enough to me that I put this in a gist : https://gist.github.com/tacaswell/9643166

    0 讨论(0)
提交回复
热议问题