0

I have a very complex function that uses matplotlib to generate an axes, in which there are various content like scatter, annotate,quiver, etc. For this reason, I do not want to touch the function, but I can use matplotlib.pyplot.gca() to get that axes. Now I call the function multiple times and get a bunch of axes. Is there a way for me to simply combine them into one figure and save it, just like in Mathematica where one can use GraphicsRow to put multiple figures together?

function()
ax1 = plt.gca()
function()
ax2 = plt.gca()
combining_them(ax1,ax2)  ???
savefig() ???
Chromatic
  • 143
  • 6
  • [Here's the pyplot tutorial that explains how to display multiple axes](https://matplotlib.org/2.0.2/users/pyplot_tutorial.html#working-with-multiple-figures-and-axes). – bunji Dec 21 '17 at 13:53
  • You need to use subplots. – Mathieu Dec 21 '17 at 14:01
  • 1
    An axes is created in a figure. If `function` produces the axes inside the same figure, there is no need to combine them. If `function` does something else, it is probably badly designed. However it is hard to help here because we don't know what `function` does at all. – ImportanceOfBeingErnest Dec 21 '17 at 14:02
  • @ImportanceOfBeingErnest Thanks. Is there a way to combine figures then? I just want to put two figures side by side. – Chromatic Dec 21 '17 at 14:05
  • 3
    `function` should be written to take an optional argument, such as `function(ax=None)`, so you can provide the axes you want `function` to plot to. You would need to add a line to function `if not ax is None: matplotlib.pyplot.sca(ax)` before any plotting stuff. You could then use `matplotlib.pyplot.subplots` to make a grid of axes. – shockburner Dec 21 '17 at 14:07
  • No, combining existing figures is not reasonable. There are some hacks that would allow such things but that would involve a lot of complicated code to get the transformations correct. The better option is for sure to *produce* the plot in a given axes, as @shockburner commented. – ImportanceOfBeingErnest Dec 21 '17 at 14:13

1 Answers1

1

As @shockburner 's comment says, function(ax=ax1) is the way to go. I just found this argument.

Chromatic
  • 143
  • 6