I have a function that creates a figure and now I would like to call this function repeatedly inside a loop with different parameters, collect the figures and plot them. This is how I would do it in julia:
using Plots
plots = Array{Plots.Plot{Plots.GRBackend},2}(undef, 3,3)
for i in 1:3
for j in 1:3
plots[i,j] = scatter(rand(10), rand(10))
title!(plots[i,j], "Plot $(i),$(j)")
end
end
plot(plots..., layout=(3,3))
However I have to write python. So currently I have a function that creates a new figure and returns it. I would be reluctant to change this function call signature (eg. to pass some axis object), since it is allready used in a different context. This is a minimal working example. For some reason the individual figures are displayed here even though I am not calling plt.display(), in the main code they are not however. Here the final figure is empty.
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(3,3)
def plottingfunction(x,y):
plt.figure()
plt.scatter(x, y)
return plt.gcf()
for i in range(3):
for j in range(3):
x = np.random.rand(10)
y = np.random.rand(10)
ax[i,j] = plottingfunction(x,y)
plt.show()
So how do I plot allready existing functions in a grid, that are for example collected inside of an array using python matplotlib.
How about making the
axparameter optional, so you can use it for this but do not break your existing use-case?