matplotlib - 从一个图形复制到另一个图形? [英] matplotlib - duplicate plot from one figure to another?

查看:85
本文介绍了matplotlib - 从一个图形复制到另一个图形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是matplotlib的新手.我想要做的是编写代码,将几个数字保存到 eps 文件中,然后生成一个复合数字.基本上我想做的是像

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() )

并从前两个函数返回的绘图中复制了所有数据和设置,但我不知道如何使用matplotlib来完成此操作.

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.

推荐答案

由于 pyplot 有点像状态机,我不确定您的要求是否可行.相反,我会考虑绘制代码,例如:

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)

这篇关于matplotlib - 从一个图形复制到另一个图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆