如何将matplotlib图形对象包含为子图?(复制)

时间:2023-01-13 15:50:43

This question already has an answer here:

这个问题已经有了答案:

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.

如何使用matplotlib图形对象作为子情节?具体地说,我有一个创建matplotlib图对象的函数,我想将它作为另一个图的子图。

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

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.

然而,不幸的是,这个操作失败。我知道,函数调用返回的每个图都是可行的——我做了一个figa.show()、…、figd.show()来确认它们是OK的。对于上面代码块中的最后一行——fig1.show()——我得到的是一个包含4个具有框架和x-和y- tickmarks/label的空图的集合。

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!

谢谢你的建议!

1 个解决方案

#1


3  

You can't put a figure in a figure.

你不能把一个数字放在一个数字里。

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

您应该修改绘图函数,以坐标轴对象作为参数。

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.

我也不清楚为什么会出现kwarg图形,我认为它是继承工作方式的产物,文档自动生成的方式,以及一些getter/setter工作的自动化方式。如果您注意到,它说figure在figure文档中没有文档,所以它可能不会做您想做的事情;)如果你深入一点,kwarg真正控制的是创建的坐标轴也被连接起来的图形,这不是你想要的。

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.

一般来说,在数据之间移动现有的坐标轴/艺术家并不容易,有太多的内部管道需要重新连接。我认为这是可以做到的,但是会涉及到内部部分,并且不能保证它将与未来的版本一起工作,或者如果内部部分以某种方式发生改变,将会破坏它,你会得到警告。

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

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.

所以,如果你传入一个坐标轴对象,它会被绘制到(你需要的新功能),但是如果你不这样做,你所有的旧代码将会按照预期工作。

#1


3  

You can't put a figure in a figure.

你不能把一个数字放在一个数字里。

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

您应该修改绘图函数,以坐标轴对象作为参数。

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.

我也不清楚为什么会出现kwarg图形,我认为它是继承工作方式的产物,文档自动生成的方式,以及一些getter/setter工作的自动化方式。如果您注意到,它说figure在figure文档中没有文档,所以它可能不会做您想做的事情;)如果你深入一点,kwarg真正控制的是创建的坐标轴也被连接起来的图形,这不是你想要的。

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.

一般来说,在数据之间移动现有的坐标轴/艺术家并不容易,有太多的内部管道需要重新连接。我认为这是可以做到的,但是会涉及到内部部分,并且不能保证它将与未来的版本一起工作,或者如果内部部分以某种方式发生改变,将会破坏它,你会得到警告。

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

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.

所以,如果你传入一个坐标轴对象,它会被绘制到(你需要的新功能),但是如果你不这样做,你所有的旧代码将会按照预期工作。