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

查看:37
本文介绍了如何将 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 中.

You can't put a figure in a 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天全站免登陆