Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
584 views
in Technique[技术] by (71.8m points)

python - matplotlib - duplicate plot from one figure to another?

I'm somewhat new to matplotlib. What I'm trying to do is write code that saves several figures to eps files, and then generates a composite figure. Basically what I'd like to do is have 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 later I could do something like

my_combo_plot( my_plot_1() , my_plot_2() )

and have all the data and settings get copied from the plots returned by the first two functions, but I can't figure out how this would be done with matplotlib.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since pyplot kind of works like a state machine, I'm not sure if what you are asking for is possible. I would instead factor out 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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...