如何包含matplotlib Figure对象作为子图? [英] How do I include a matplotlib Figure object as subplot?

查看:237
本文介绍了如何包含matplotlib Figure对象作为子图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用matplotlib Figure对象作为子图?具体来说,我有一个创建matplotlib Figure对象的函数,我想将此作为子图包含在另一个Figure中.

How can I use a matplotlib Figure object as a subplot? Specifically, I have a function that creates a matplotlib Figure object, and I would like to include this as a subplot in another Figure.

简而言之,这是我尝试过的简化代码:

In short, here's stripped-down pseudocode for what I've tried:

    fig1 = plt.figure(1, facecolor='white')
    figa = mySeparatePlottingFunc(...)
    figb = mySeparatePlottingFunc(...)
    figc = mySeparatePlottingFunc(...)
    figd = mySeparatePlottingFunc(...)
    fig1.add_subplot(411, figure=figa)
    fig1.add_subplot(412, figure=figb)
    fig1.add_subplot(413, figure=figc)
    fig1.add_subplot(414, figure=figd)
    fig1.show()

可惜的是,这失败了.我知道从函数调用返回的各个图是可行的-我做了一个figa.show(),...,figd.show()来确认它们是可以的.我在上面的代码块的最后一行得到的结果是--fig1.show()-是 四个空图的集合,这些图具有框架以及x和y勾号/标签.

Sadly, however, this fails. I know for a fact the individual plots returned from the function invocations are viable--I did a figa.show(),...,figd.show() to confirm that they are OK. What I get for the final line in the above code block--fig1.show()--is a collection of four empty plots that have frames and x- and y- tickmarks/labels.

我已经做了大量的谷歌搜索,并进行了广泛的实验,但是很明显,我错过了一些非常微妙或令人尴尬的显而易见的东西(我很高兴能成为后者因为我会被卡住).

I've done quite a bit of googling around, and experimented extensively, but it's clear that I've missed something that is either really subtle, or embarrassingly obvious (I'll be happy for it to be the latter as long as I can get un-stuck).

感谢您提供的任何建议!

Thanks for any advice you can offer!

推荐答案

您不能将figure放在figure中.

您应该修改绘图函数以将axes对象作为参数.

You should modify your plotting functions to take axes objects as an argument.

我也不清楚为什么kwarg figure在那里,我认为这是继承工作方式,文档自动生成方式以及一些getter/setter工作方式的产物.自动化.如果您注意到,它说figureFigure文档中没有记录,因此它可能无法满足您的要求;).如果稍微深入一点,kwarg真正控制的就是创建的轴也已附加的图形,这不是您想要的.

I am also unclear why the kwarg figure is there, I think it is an artifact of the way that inheritance works, the way that the documentation is auto-generated, and the way some of the getter/setter work is automated. If you note, it says figure is undocumented in the Figure documentation, so it might not do what you want;). If you dig down a bit, what that kwarg really controls is the figure that the created axes is attached too, which is not what you want.

通常,在图形之间移动现有的轴/艺术家并不容易,内部管道太多,需要重新连接.我认为是可以做到的,但是要涉及到内部结构,并且不能保证它会与将来的版本一起使用,或者不能保证内部结构的改变会破坏它,您将得到警告.

In general, moving existing axes/artists between figures is not easy, there are too many bits of internal plumbing that need to be re-connected. I think it can be done, but will involving touching the internals and there is no guarantee that it will work with future versions or that you will get warning if the internals change in a way that will break it.

您需要使用绘图功能将Axes对象作为参数.您可以使用以下模式:

You need to your plotting functions to take an Axes object as argument. You can use a pattern like:

def myPlotting(..., ax=None):
    if ax is None:
        # your existing figure generating code
        ax = gca()

因此,如果您传递Axes对象,它会被吸引(您需要的新功能),但是如果您不这样做,那么所有旧代码将无法按预期工作.

so if you pass in an Axes object it gets drawn to (the new functionality you need), but if you don't all of your old code will work as expected.

这篇关于如何包含matplotlib Figure对象作为子图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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