python - debugging: loop for plotting isnt showing the next plot

2024/11/18 9:41:23

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

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.

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 # indicefor 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 colorbarmean_loc = (L_B_1_mean[label] - cb.vmin) / (cb.vmax - cb.vmin)# add a horizontal line to the colorbar axiscb.ax.hlines(mean_loc, 0, 1, label="Mean")#Columns conditionj=j+1k=k+1if j==3:print "entered if condition: new line"i=i+1j=0if i==2:     # Plot is full, resetting for next plotj=0i=0# deleting axes in case there are no 6 files leftif 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()

Files:

all_files looks like this:

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: (How can I upload a file, so you could run the code?)

2009161 [[ 3.58403872  3.57167339  3.60095971 ...,  7.01769751  6.809439216.88883769][ 3.40962239  3.51960881  3.54954594 ...,  6.97864908  6.894044147.03657092][ 3.26384158  3.51244993  3.63089684 ...,  6.89729818  6.994919267.11922343]..., [ 8.26724734  8.05183015  7.79801534 ...,  8.21583357  8.243167478.24772312][ 8.17288029  8.11691087  7.66655229 ...,  8.21648437  8.202817428.09998989][ 8.26659653  7.93012921  7.49929484 ...,  8.19305531  8.188499668.10845038]]
2009065 [[ 6.12674245  6.25950712  6.74045364 ...,  7.12377909  7.110762947.0977468 ][ 6.08509079  6.22761757  6.86215459 ...,  7.13093796  7.122477477.11336617][ 6.07728111  6.19702963  6.61745108 ...,  7.13939846  7.125731517.11727101]..., 
Answer

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.

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

https://en.xdnf.cn/q/118696.html

Related Q&A

Return formatted string in Python

I have a string:testString = """ My name is %s and I am %s years old and I live in %s"""I have code that finds these three strings that I want to input into testString. Ho…

How to get the greatest number in a list of numbers using multiprocessing

I have a list of random numbers and I would like to get the greatest number using multiprocessing. This is the code I used to generate the list: import random randomlist = [] for i in range(100000000):…

python pandas yahoo stock data error

i am try to pullout intraday aapl stock data by yahoo. but there problem i facing with my program..import pandas as pd import datetime import urllib2 import matplotlib.pyplot as plt get = http://chart…

Web Scraping Stock Ticker Price from Yahoo Finance using BeautifulSoup

Im trying to scrape Gold stock ticker from Yahoo! Finance. from bs4 import BeautifulSoup import requests, lxmlresponse = requests.get(https://finance.yahoo.com/quote/GC=F?p=GC=F) soup = BeautifulSoup(…

How to convert the radius from meter to pixel?

I have a camera with these specs:full resolution 1280x1024 pixel size 0.0048mm focal length 8 mmI need to detect a ball in this image. It is 4 meters away and its radius is 0.0373 meter. How to convert…

Calculting GPA using While Loop (Python)

A GPA, or Grade point Average, is calculated by summing the grade points earned in a student’s courses and then dividing by the total units. The grade points for an individual course are calculated by…

Return function that modifies the value of the input function

How can I make a function that is given a function as input and returns a function with the value tripled. Here is some pseudo code for what Im looking for. Concrete examples in Python or Scala would b…

how to access the list in different function

I have made a class in which there are 3 functions. def maxvalue def min value def getActionIn the def maxvalue function, I have made a list of actions. I want that list to be accessed in def getaction…

Replacing numpy array with max value [duplicate]

This question already has answers here:numpy max vs amax vs maximum(4 answers)Closed 2 years ago.I have an array, a = np.array([[0,9,8],[5,6,4]])how to replace the each array in axis 1 with the max val…

Finding the max and min in dictionary as tuples python

so given this dictionary im trying to find the max value and min value {Female :[18,36,35,49,19],Male :[23,22,6,36,46]}the output should be in tuples for example key: (min,max)Female: (18,49) Male: (6,…