matplotlib-从一个图形复制到另一个图形?

2024-06-17 10:42:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我对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
    ...

后来我在哪里可以做些类似的事情

my_combo_plot( my_plot_1() , my_plot_2() )

并让所有的数据和设置从前两个函数返回的绘图中复制出来,但是我不知道如何使用matplotlib完成这项工作。


Tags: gs图形returnplotmatplotlibmydeffig
1条回答
网友
1楼 · 发布于 2024-06-17 10:42:45

因为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)

相关问题 更多 >