matplotlib - 从一个图到另一个图的重复图?

Ale*_*x Z 9 python matplotlib

我对matplotlib有些新意.我要做的是编写代码,将几个数字保存到eps文件,然后生成一个复合图形.基本上我想做的就是有类似的东西

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
    ...
Run Code Online (Sandbox Code Playgroud)

之后我可以做点什么

my_combo_plot( my_plot_1() , my_plot_2() )
Run Code Online (Sandbox Code Playgroud)

并且拥有所有的数据和设置也会从由前两个函数返回的情节抄袭,但我无法弄清楚如何做到这一点与matplotlib来完成.

wim*_*wim 7

由于pyplot类似于状态机,我不确定你所要求的是否可行.我会反过来考虑绘图代码,如下所示:

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)
Run Code Online (Sandbox Code Playgroud)

  • 这可能是一种快速解决方法,但不是答案。如果您有花费大量时间的“绘图说明”,则此解决方案不可行。假设我有一个3D图,并想在3个子图中显示XZ,YZ和XY视图。原则上,您可以使用相同的图并更改视图。使用此解决方案,您只需绘制3次而不是一次,即可更改视图。(不幸的是,我也不知道该如何实现)。有人吗 (2认同)