我需要调试方面的帮助。我只是不知道为什么它不能按预期工作。
下面的代码应以6的块为单位读取数据文件(名称存储在all_files
中),并将它们放在子图中(i,j索引0,0左上角,1,2右下角),显示图。
它适用于前六个文件名,并显示绘图。当我关闭绘图窗口时,代码继续执行并结束而没有显示下一个绘图... plt.show()
是否在错误的位置?
fig, axes = plt.subplots(nrows=cond_row, ncols=cond_col)
k=0 # step
i=0 # indice
j=0 # indice
for z in range(0, len(all_files), 6):
for fe in all_files[z:z+6]:
get_name_tag = re.findall(".A(\d{7}).", all_files[k])[0]
for label, values in data_1_band.items():
if label == str(get_name_tag):
axes[i, j].set_title("Band " + str(Band_names[specific_band]) +" - "+ "file: "+ str(label))
axes[i, j].set_ylabel("Along-Track Scans")
axes[i, j].set_xlabel("Along-Scan Scans")
im1 = axes[i, j].imshow(values, interpolation="none")
cb = fig.colorbar(im1, ax=axes[i, j], label='W $\mathrm{m^{-2}}$ $\mathrm{\mu m^{-1}}$ $\mathrm{ster^{-1}}$')
# Taking mean from data and finds location on colorbar
mean_loc = (L_B_1_mean[label] - cb.vmin) / (cb.vmax - cb.vmin)
# add a horizontal line to the colorbar axis
cb.ax.hlines(mean_loc, 0, 1, label="Mean")
#Columns condition
j=j+1
k=k+1
if j==3:
print "entered if condition: new line"
i=i+1
j=0
if i==2: # Plot is full, resetting for next plot
j=0
i=0
# deleting axes in case there are no 6 files left
if len(all_files) < 4:
fig.delaxes(axes[1][0])
fig.delaxes(axes[1][1])
fig.delaxes(axes[1][2])
if len(all_files) < 5:
fig.delaxes(axes[1][1])
fig.delaxes(axes[1][2])
if len(all_files) < 6:
fig.delaxes(axes[1][2])
plt.show()
文件:
all_files
看起来像这样:
MYD021KM.A2009049.0930.006.2012060021525.hdf
MYD021KM.A2010305.0900.006.2012070143225.hdf
MYD021KM.A2010321.0900.006.2012071073516.hdf
MYD021KM.A2010337.0900.006.2012071175047.hdf
MYD021KM.A2010353.0900.006.2012072063847.hdf
MYD021KM.A2010001.0900.006.2012065165320.hdf
.....
data_1_band
:(如何上传文件,以便您可以运行代码?)
2009161 [[ 3.58403872 3.57167339 3.60095971 ..., 7.01769751 6.80943921
6.88883769]
[ 3.40962239 3.51960881 3.54954594 ..., 6.97864908 6.89404414
7.03657092]
[ 3.26384158 3.51244993 3.63089684 ..., 6.89729818 6.99491926
7.11922343]
...,
[ 8.26724734 8.05183015 7.79801534 ..., 8.21583357 8.24316747
8.24772312]
[ 8.17288029 8.11691087 7.66655229 ..., 8.21648437 8.20281742
8.09998989]
[ 8.26659653 7.93012921 7.49929484 ..., 8.19305531 8.18849966
8.10845038]]
2009065 [[ 6.12674245 6.25950712 6.74045364 ..., 7.12377909 7.11076294
7.0977468 ]
[ 6.08509079 6.22761757 6.86215459 ..., 7.13093796 7.12247747
7.11336617]
[ 6.07728111 6.19702963 6.61745108 ..., 7.13939846 7.12573151
7.11727101]
...,
答案 0 :(得分:0)
问题很可能是plt.show()在循环内部。通常,在创建所有图形之后,仅应调用一次。因为一旦调用,matplotlib认为它已经完成。
要解决此问题,我不得不在第一个for循环内移动fig, axes = plt.subplots…
,并在plt.show()之后添加fig.close()。