我正在尝试使用下面的代码绘制16个子图,但是即使我的while条件的长度相同,我也遇到了IndexError: list index out of range
错误……为什么?
import matplotlib.pyplot as plt
x_new = [5436, 7]
y_new = [[0.040028619784490037, 0.064413738530207612, 0.062815762675466444, 0.073433012173153914, 0.032932640559388944, 0.093132462472780564, 0.86465850284943391, 1.6305790768438158, 2.9282502771409802, 2.2143984435689292, 3.763690626719586, 3.7678535429124818, 3.1207650603328103, 2.661000804740647, 2.323640314964067, 2.3603270205788753], [0.028737592073923207, 0.044262388955502406, 0.046552654094977768, 0.056216098251445794, 0.032034316764190607, 0.07977527592601974, 0.8403112875093226, 1.5060638978019212, 2.5880236300872195, 2.1269536152167832, 3.4154460490965977, 3.4493404237456957, 2.9860435093537485, 2.618519230197268, 2.3377423748500568, 2.4191266598989216]]
# Sorting Data
idx = np.argsort(np.array(x_new))
y_new = np.array(y_new)[idx]
x_new = np.array(x_new)[idx]
fig = plt.figure()
fig.subplots_adjust(hspace=0.4, wspace=0.4)
i=1
j=0
subplotnr = range(1,17,1)
while j <= 17:
print "i, j: ", i, j
ax = fig.add_subplot(4, 4, subplotnr[i])
# Data handling
y_new_plot = []
for ch in range(len(x_new)):
y_new_plot.append(y_new[ch][j])
print y_new_plot
# Plotting Subplots
ax.plot(x_new, y_new_plot)
i=i+1
j=j+1
plt.show()
答案 0 :(得分:0)
您的代码有很多问题,
y_new = [[ 0.02873759 0.04426239 0.04655265 ...
在python上不起作用j <= range(0,16,1)
没有道理plt.show()
将在每一步呈现进度这是一个更简单的版本,您可以适应自己的需求
import matplotlib.pyplot as plt
def subplot(ax, i, j):
# plot the (i, j)-subplot here
x = [1, 2]
y = [3, 4]
ax.plot(x, y)
fig = plt.figure()
fig.set_figheight(12)
fig.set_figwidth(12)
for i in range(4):
for j in range(4):
if i == 0 and j == 3: continue
ax = plt.subplot2grid((4, 4), (i, j))
subplot(ax, i, j)
plt.show()
编辑
回答OP的新问题
import matplotlib.pyplot as plt
x_new = [5436, 7]
y_new = [[0.040028619784490037, 0.064413738530207612, 0.062815762675466444, 0.073433012173153914, 0.032932640559388944, 0.093132462472780564, 0.86465850284943391, 1.6305790768438158, 2.9282502771409802, 2.2143984435689292, 3.763690626719586, 3.7678535429124818, 3.1207650603328103, 2.661000804740647, 2.323640314964067, 2.3603270205788753], [0.028737592073923207, 0.044262388955502406, 0.046552654094977768, 0.056216098251445794, 0.032034316764190607, 0.07977527592601974, 0.8403112875093226, 1.5060638978019212, 2.5880236300872195, 2.1269536152167832, 3.4154460490965977, 3.4493404237456957, 2.9860435093537485, 2.618519230197268, 2.3377423748500568, 2.4191266598989216]]
# Sorting Data
idx = np.argsort(np.array(x_new))
y_new = np.array(y_new)[idx]
x_new = np.array(x_new)[idx]
fig = plt.figure()
fig.subplots_adjust(hspace=0.4, wspace=0.4)
i=1
j=0
subplotnr = range(0, 17,1)
while j <= 15:
#print("i, j: ", i, j)
ax = fig.add_subplot(4, 4, subplotnr[i])
y_new_plot = []
for ch in range(len(x_new)):
y_new_plot.append(y_new[ch][j])
#print(y_new_plot)
ax.plot(x_new, y_new_plot)
i=i+1
j=j+1
plt.show()