You can zip
your models and axes together and loop over both at the same time. However, because your subplots come as a 2d array
, you first have to 'linearize' its elements. You can easily do that by using the reshape
method for numpy
arrays. If you give that method the value -1
it will convert the array into a 1d
vector. For lack of your input data, I made an example using mathematical functions from numpy
. The funny getattr
line is only there so that I was easily able to add titles to the plots:
from matplotlib import pyplot as plt
import numpy as np
modelInfo = ['sin', 'cos', 'tan', 'exp', 'log', 'sqrt']
f, axarr = plt.subplots(2,3)
x = np.linspace(0,1,100)
for model, ax in zip(modelInfo, axarr.reshape(-1)):
func = getattr(np, model)
ax.plot(x,func(x))
ax.set_title(model)
f.tight_layout()
plt.show()
The result looks like this:
.
Note that, if your number of models exceeds the number of available subplots
, the excess models will be ignored without error message.
Hope this helps.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…