我正在使用 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
谢谢