调试的循环不会显示下一个绘图 [英] python - debugging: loop for plotting isn't showing the next plot

查看:51
本文介绍了调试的循环不会显示下一个绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要调试方面的帮助.我只是不知道为什么它不能按预期工作.

I need help in debugging. I just can't figure out why it's not working as expected.

下面的代码应读取6个数据块(名称存储在all_files中)的数据文件,并将其布置在子图中(i,j索引0,0左上角,1,2右下角),显示图.

The Code below should read data files (names are stored in all_files) in chunks of 6, arrange them in subplots (i,j indices 0,0 top left, 1,2 bottom right), show plot.

它适用于前六个文件名,并显示绘图.当我关闭绘图窗口时,代码继续执行并结束而没有显示下一个绘图... plt.show()在错误的位置吗?

It works for the first six file names and the plot is being showed. When I close the window of the plot, the code continues and ends without showing the next plot... is plt.show() at a wrong place?

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 :(如何上传文件,以便您可以运行代码?)

data_1_band: (How can I upload a file, so you could run the code?)

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]
 ..., 

推荐答案

问题很可能是plt.show()在循环内部.通常,在创建所有图形之后,仅应调用一次.因为一旦调用,matplotlib便认为它已经完成.

The problem is likely that plt.show() is inside the loop. Generally, it should only be called once, after all the figures have been created. Because once it’s called, matplotlib thinks it’s done.

要解决此问题,我必须在第一个for循环内移动fig, axes = plt.subplots…,并在plt.show()之后添加fig.close().

To solve this, I had to move fig, axes = plt.subplots… inside the first for-loop, and add a fig.close() after plt.show().

这篇关于调试的循环不会显示下一个绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆