How to include matplotlib Figure object as subtitle?

How to use matplotlib Figure object object as subtitle? In particular, I have a function that creates a matplotlib drawing object, and I would like to include it as a subtitle in another drawing.

In short, here's a stripped down pseudo-code for what I 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() 

Unfortunately, this fails. I know that in fact, individual plots returned from the invocations functions are viable - I did figa.show (), ..., figd.show () to confirm that they are fine. What I get for the last line in the above code block - fig1.show () - is a set of four empty graphs that have frames and x- and y-tags / tags.

I did quite a lot of search queries and experimented a lot, but it is clear that I missed something that is either very subtle or embarrassing (I will be happy that it will be the last as long as I can get stuck).

Thanks for any advice you can offer!

+4
source share
1 answer

You cannot put figure in figure .

You must modify your build functions to accept axes objects as an argument.

I also don’t understand why kwarg figure exists, I think this is an artifact of how inheritance works, the way to automatically generate documentation and how some of the recipients / setters work is automated. If you notice, he says that figure documented in the figure documentation, so it may not do what you want;). If you dig a little, what kwarg really controls is the number in which the created axes are also created. This is not what you want.

In general, moving existing axes / artists between pieces is not easy, there are too many bits of internal plumbing that need to be reconnected. I think it can be done, but it will include touching the internal components, and there is no guarantee that it will work with future versions or that you will receive a warning if the internal changes change so that it breaks it.

You need your graphing functions to accept the axes object as an argument. You can use a template, for example:

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

therefore, if you pass the axes object to which it is drawn (you need new functionality), but if you are not all, then your old code will work as expected.

+3
source

Source: https://habr.com/ru/post/1482719/


All Articles