0

I have a function that generates a figure with 4 subplots:

def genFigures(self,savefile='out/histograms.png',show=0) :
    fig, ((barsimpF, barsimpT),(barsworF, barsworT)) = plt.subplots(2, 2, sharey=True,figsize=(12,10))

but I would like to have the opportunity to reuse and recombine the subplot in different ways later on, with other subplots from other calls to the function. I have tried the function to return the subplots

return (barsimpF,barsimpT)

But how do I reuse them later on? Thanks.

andrea m.
  • 668
  • 7
  • 15
  • What exactly do you mean by "reuse"? You can use them, just by calling some of the axes' methods, e.g. `barsimpF.set_xlim([1,5])` or so. – ImportanceOfBeingErnest Mar 30 '18 at 02:00
  • I mean to create another figure with them and others recombined in different order. Would this work? fig, (barsimpT,barsimpF) = plt.subplots(1,2) – andrea m. Mar 30 '18 at 10:52
  • This would overwrite the existing variables, like `a = 3; a=2` results in `a` being `2`; and not `2` being `3`. Subplots are necessarily bound to a figure. You cannot have the same subplot in more than one figure. Best do not use functions which create subplots, but use functions which take subplots as argument to manipulate them. – ImportanceOfBeingErnest Mar 30 '18 at 10:58
  • Thanks, I think I understand why. I am struggling to understand how to create a "function that takes a subplot as argument". Sorry the documentation is not very helpful on this. I need to automate the creation of many figures and I still would like the ability to recombine some subplots on the fly without having to regenerate the data. – andrea m. Mar 30 '18 at 11:52
  • Please have a look at [this question](https://stackoverflow.com/questions/47098845/matplotlib-pyplot-create-a-subplot-of-stored-plots) as well as [this question](https://stackoverflow.com/questions/45861656/plot-something-in-one-figure-and-use-it-again-later-for-another-figure) to see what I mean by "function that takes a subplot as argument". – ImportanceOfBeingErnest Mar 30 '18 at 12:07
  • Thanks, that helps, especially the second link – andrea m. Mar 30 '18 at 14:49

0 Answers0