如何在一种布局中排列多个pyplot图形? [英] How could I arrange multiple pyplot figures in a kind of layout?

查看:35
本文介绍了如何在一种布局中排列多个pyplot图形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个软件来进行信号分析.有多个功能,每个功能最终都显示一个复杂的图形,其中包含标签,绘图,axhspan,axvspan等.通常,这些功能被单独调用.我的每个函数都返回一个图形对象,例如我可以将其保存为 pdf.

I created a software to do signal analyses. There are multiple functions, and each finally display a complex figure containing labels, plot, axhspan, axvspan etc... Usually, these functions are called individually. Everyone of my functions returns a figure object, that I can save in pdf for example.

def Myfunction1(self):
    fig = pyplot.figure()
    ...do somestuff, create my figure
    pyplot.show()
    fig.savefig('C:\MyFigurefolder\figure1.pdf', dpi=300)
    return fig

def Myfunction2(self):
    fig = pyplot.figure()
    ...do some other stuff, create my 2nd figure
    pyplot.show()
    fig.savefig('C:\MyFigurefolder\figure2.pdf', dpi=300)
    return fig

现在,我想通过进行荟萃分析,然后将多个图形合并在一起,然后将它们保存在最终的pdf文件中,来创建一种摘要图形".我真的不知道我该怎么做.有没有一种方法可以使用整个图形对象(或者可能是多个单独的pdf)来完成我的图形?

Now, I would like to create a kind of "summary figure", by doing a metaanalyses, and pooling multiple figures together, and saving them in a final pdf. I don't really know how I should do that. Is there a way to use whole figure objects, (or maybe the multiple individual pdf) to do my figure?

类似的东西:

def FinalFigure(self):

    final = A_Kind_Of_Layout_Or_A_Figure_or_something

    a=self.Myfunction1()
    b=self.Myfunction2()

    Action_to_arrange_a_and_b_like_gridspec

    final.savefig('C:\MyFigurefolder\FinalFigure.pdf', dpi=300)

推荐答案

您可以将多个图与matplotlib.pyplot.subplot结合使用.如需对布局进行更多控制,请查看 GridSpec.

You may combine several plots with matplotlib.pyplot.subplot. For more control on the layout, check out the GridSpec.

编辑:根据要求,提供了链接教程中的一个简短示例:

As requested, a short example from the linked tutorial:

gridspec实例提供了类似于数组(2d或1d)的索引,该索引返回SubplotSpec实例.对于跨越多个单元格的 SubplotSpec,使用 slice.

A gridspec instance provides array-like (2d or 1d) indexing that returns the SubplotSpec instance. For, SubplotSpec that spans multiple cells, use slice.

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
gs = gridspec.GridSpec(3, 3)
ax1 = plt.subplot(gs[0, :])
ax2 = plt.subplot(gs[1,:-1])
ax3 = plt.subplot(gs[1:, -1])
ax4 = plt.subplot(gs[-1,0])
ax5 = plt.subplot(gs[-1,-2])

这篇关于如何在一种布局中排列多个pyplot图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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