Search code examples
imagematplotlibtextlabel

how to add text labels to figs and merge them together


I have two figs fig1.png and fig2.png made of matplotlib in python code, now in some papers, I want to merge these two figs together, and give them (a) and (b) text labels in the top left area, respectively. How can I use matplotlib do it? I have the codes to generate fig1.png and fig2.png with fig1.py,fig2.py, is there any other convenient way ?like write another short code to do it?


Solution

  • How easily you can combine two figures depends on how complex they are. If they are both simple figures with single axes it is relatively straightforward.

    Since you have the code to generate the two axes, I would start by putting that code into a single file and then modifying your code to plot to subplot axes.

    You can layout multiple axes within a figure as follows:

    from matplotlib.pyplot import plt
    fig, ax = plt.subplots(1,2) #nrows, ncols
    

    The variable ax is now a list of two axes onto which you can plot.

    Subplot Frames

    So for example, to plot to the left hand side you can use:

    ax[0].plot([1,2,3,4,5]
    

    To the right:

    ax[1].plot([1,2,3,4,5]
    

    You can also set labels on the subplots using .set_title(). A complete example:

    ax[0].plot([1,2,3,4,5])
    ax[0].set_title("A")
    ax[1].plot([5,4,3,2,1])
    ax[1].set_title("B")
    

    Completed plot