Matplotlib - duplicate plot from one figure to another?

I am a little new to matplotlib. What I'm trying to do is write code that saves a few digits in eps files and then generates a composite figure. Basically, I would like to do something like

def my_plot_1():
    fig = plt.figure()
    ...
    return fig.

def my_plot_2():
    fig = plt.figure()
    ...
    return fig

def my_combo_plot(fig1,fig2):
    fig = plt.figure()
    gs = gridspec.GridSpec(2,2)
    ax1 = plt.subplot(gs[0,0])
    ax2 = plt.subplot(gs[0,1])
    ax1 COPY fig1
    ax2 COPY fig2
    ...

where then I could do something like

my_combo_plot( my_plot_1() , my_plot_2() )

and all the data and settings will be copied from the graphs returned by the first two functions, but I can’t understand how this will be done using matplotlib.

+5
source share
1 answer

Since works like pyplot are like a state machine, I'm not sure what you are asking for is possible. I would instead highlight the drawing code, something like this:

import matplotlib.pyplot as plt

def my_plot_1(ax=None):
    if ax is None:
      ax = plt.gca()
    ax.plot([1, 2, 3], 'b-')

def my_plot_2(ax=None):
    if ax is None:
      ax = plt.gca()
    ax.plot([3, 2, 1], 'ro')

def my_combo_plot():
    ax1 = plt.subplot(1,2,1)
    ax2 = plt.subplot(1,2,2)
    my_plot_1(ax1)
    my_plot_2(ax2)
+5
source

All Articles