How do I include a matplotlib Figure object as subplot? [duplicate]

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.

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.

Leave a Comment