Python 打印对象中的matplotlib子绘图

Python 打印对象中的matplotlib子绘图,python,matplotlib,plot,Python,Matplotlib,Plot,我有一系列返回三个绘图对象(figure、axis和plot)的函数,我想将它们作为子绘图组合成一个图形。我将示例代码放在一起: import matplotlib.pyplot as plt import numpy as np def main(): line_fig,line_axes,line_plot=line_grapher() cont_fig,cont_axes,cont_plot=cont_grapher() compound_fig=plot_c

我有一系列返回三个绘图对象(figure、axis和plot)的函数,我想将它们作为子绘图组合成一个图形。我将示例代码放在一起:

import matplotlib.pyplot as plt
import numpy as np

def main():

    line_fig,line_axes,line_plot=line_grapher()
    cont_fig,cont_axes,cont_plot=cont_grapher()

    compound_fig=plot_compounder(line_fig,cont_fig)#which arguments?

    plt.show()

def line_grapher():
    x=np.linspace(0,2*np.pi)
    y=np.sin(x)/(x+1)

    line_fig=plt.figure()
    line_axes=line_fig.add_axes([0.1,0.1,0.8,0.8])
    line_plot=line_axes.plot(x,y)
    return line_fig,line_axes,line_plot

def cont_grapher():
    z=np.random.rand(10,10)

    cont_fig=plt.figure()
    cont_axes=cont_fig.add_axes([0.1,0.1,0.8,0.8])
    cont_plot=cont_axes.contourf(z)
    return cont_fig,cont_axes,cont_plot

def plot_compounder(fig1,fig2):
    #... lines that will compound the two figures that
    #... were passed to the function and return a single
    #... figure
    fig3=None#provisional, so that the code runs
    return fig3

if __name__=='__main__':
    main()

将一组图形与一个函数组合在一起将非常有用。以前有人这样做过吗?

如果要在同一个图形上绘制图形,则无需为每个图形创建图形。将打印功能更改为仅返回轴,可以实例化具有两个子地块的地物,并向每个子地块添加轴:

def line_grapher(ax):
    x=np.linspace(0,2*np.pi)
    y=np.sin(x)/(x+1)

    ax.plot(x,y)


def cont_grapher(ax):
    z=np.random.rand(10,10)

    cont_plot = ax.contourf(z)

def main():

    fig3, axarr = plt.subplots(2)
    line_grapher(axarr[0])
    cont_grapher(axarr[1])

    plt.show()


if __name__=='__main__':
    main()

查看用于在一个图形上绘制多个绘图的
plt.subplot
函数和
add_subplot
figure方法。

谢谢,问题是,我不能将轴作为参数传递给函数,我的绘图函数每次都返回这三个对象。我尝试过:
axarr[0]=line_grapher()[1];axarr[1]=cont_grapher()[1]
但是我得到了三个数字。如果您的函数正在调用
plt.figure
,那么它们将创建它们的数字。为什么您的函数需要返回这三个对象?我正在使用两个已经存在的函数,并使用一些参数来创建定制的图形。我不能用我正在做的来修改它们,所以我不能将轴作为参数传递。