0

I generated several matplotlib.figure.Figure instances from my customizaed function (just like the fig returns from plt.subplots()), but now I want to group them together as a large Figure without needing to rewrite the whole function, is it possible?

fig1, axes1 = plt.subplots()
fig2, axes2 = plt.subplots()

Is it possible to have some function to:

fig3 = some_function(fig1, fig2)

I have searched for similar questions and they usually points to plt.subplots(), I fully recognize I can do so with plt.subplots but I then need to rewrite everything, therefore I am asking here. Thanks in advance!

neurothew
  • 185
  • 1
  • 2
  • 11
  • Does this answer your question? [How do I get multiple subplots in matplotlib?](https://stackoverflow.com/questions/31726643/how-do-i-get-multiple-subplots-in-matplotlib) – Trenton McKinney Jul 27 '20 at 04:08
  • [matplotlib.pyplot.subplots](https://matplotlib.org/3.3.0/api/_as_gen/matplotlib.pyplot.subplots.html) – Trenton McKinney Jul 27 '20 at 04:09
  • @TrentonMcKinney not really, I know plt.subplots can do it but my purpose is to combine the Figure instances return by plt.subplots(). – neurothew Jul 27 '20 at 04:31

2 Answers2

0

Rather than combine generated figures, you should combine generated plots. The matplotlib.figure library functions very similarly to the matplotlib.pyplots library. Since you are instantiating a matplotlib.figure.Figure instance, you would generate some variation of the following basic code:

fig = Figure(figsize=(10, 10), dpi=100)

The matplotlib.figure.Figure class has function called add_subplot that functions like plt.subplots():

graph = fig.add_subplot(111)

With this foundation, you can add as many plots as you wish:

for i in data:
    graph.plot(data[x_values[i]], data[y_values[i]])

Note: data is just an arbitrary pandas DataFrame that can store all of your points.

0

Thanks for you guys help but the answers did not really correspond to my question. I knew that one shall use plt.subplots() but my question is about combining Figures after they have been created.

I didnt find a workaround, and just rewrite everything and adapt to plt.subplots().

neurothew
  • 185
  • 1
  • 2
  • 11