【问题标题】:python - debugging: loop for plotting isn't showing the next plotpython - 调试:绘图循环未显示下一个绘图
【发布时间】:2019-04-04 06:10:55
【问题描述】:

我需要调试方面的帮助。我只是想不通为什么它没有按预期工作。

下面的代码应该以 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]
 ..., 

【问题讨论】:

    标签: python for-loop matplotlib


    【解决方案1】:

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

    为了解决这个问题,我不得不将fig, axes = plt.subplots… 移动到第一个 for 循环中,并在 plt.show() 之后添加一个 fig.close()。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-19
      • 2021-08-11
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多