我正在使用matplotlib在循环中进行子绘图。例如,我想要绘制49个数据集,并从文档中,我这样实现它;
import numpy as np
import matplotlib.pyplot as plt
X1=list(range(0,10000,1))
X1 = [ x/float(10) for x in X1 ]
nb_mix = 2
parameters = []
for i in range(49):
param = []
Y = [0] * len(X1)
for j in range(nb_mix):
mean = 5* (1 + (np.random.rand() * 2 - 1 ) * 0.5 )
var = 10* (1 + np.random.rand() * 2 - 1 )
scale = 5* ( 1 + (np.random.rand() * 2 - 1) * 0.5 )
Y = [ Y[k] + scale * np.exp(-((X1[k] - mean)/float(var))**2) for k in range(len(X1)) ]
param = param + [[mean, var, scale]]
ax = plt.subplot(7, 7, i + 1)
ax.plot(X1, Y)
parameters = parameters + [param]
ax.show()
但是,从i = 0开始,我的索引超出了范围错误。
我能在哪里做得更好?
- 这是我得到的错误
Traceback (most recent call last):
File "F:\WORK\SOLVEUR\ALGOCODE\PYTHON_\DataSets\DataSets_DONLP2_gaussians.py", line 167, in <module>
ax = plt.subplot(7, 7, i + 1)
File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 766, in subplot
a = fig.add_subplot(*args, **kwargs)
File "C:\Python27\lib\site-packages\matplotlib\figure.py", line 777, in add_subplot
a = subplot_class_factory(projection_class)(self, *args, **kwargs)
File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 8364, in __init__
self._subplotspec = GridSpec(rows, cols)[int(num)-1]
File "C:\Python27\lib\site-packages\matplotlib\gridspec.py", line 175, in __getitem__
raise IndexError("index out of range")
IndexError: index out of range
由于