python plotting overrides data
Question: I have lot of binary and ascii files in one folder. I am reading them using
glob module. Doing processing of the binary data so that I can plot them. And
finally, I am trying to plot simplified binary data in one subplot and normal
ascii file in another subplot. The problem I am facing is that it can generate
plots for the corresponding binary files. But for the ascii files it just
simply override the previous files and always generates the same plot. Here is
the simplied version of the code for an example-

    
    
    import glob 
    import numpy as np
    from struct import unpack
    import matplotlib.pyplot as plt
    
    chi = sorted(glob.glob('C:/Users/Desktop/bin/*.chi'))
    
    for index,fh in enumerate(chi):
        data = np.genfromtxt(fh, dtype = float)
        x = [row[0] for row in data]
        y = [row[1] for row in data]
    
    binary = sorted(glob.glob('C:/Users/Desktop/bin/*.bin'))
    
    for count,FILE in enumerate(binary):
        F = open(FILE,'rb')
        B = unpack('f'*1023183, F.read(4*1023183))
        A = np.array(B).reshape(1043, 981)
        F.close()
    
        #a = something column 1   # some further processing
        #b =  something column 2  # and generates 1D data
    
        fig = plt.figure(figsize=(11, 8.0))
    
        ax1 =fig.add_subplot(211,axisbg='w')
        ax1.plot(a,b)
    
        ax2 =fig.add_subplot(212, axisbg ='w')
        ax2.plot(x,y)
    
        plt.show()
    

Can somebody please explain why the files are replacing each other during
plotting only for one set of data where the other set is plotting correctly?


Answer: the structures of the loops is not correct in your example, you must have the
plot command inside the loop over the ascii file, else only the last one is
plotted. This should work:

try it like this:

    
    
    import glob 
    import numpy as np
    from struct import unpack
    import matplotlib.pyplot as plt
    
    fig = plt.figure(figsize=(11, 8.0))
    
    chi = sorted(glob.glob('C:/Users/Desktop/bin/*.chi'))
    
    for index,fh in enumerate(chi):
        data = np.genfromtxt(fh, dtype = float)
        x = [row[0] for row in data]
        y = [row[1] for row in data]
    
        ax1 =fig.add_subplot(211, axisbg ='w')
        ax1.plot(x,y)
    
    
    binary = sorted(glob.glob('C:/Users/Desktop/bin/*.bin'))
    
    for count,FILE in enumerate(binary):
        F = open(FILE,'rb')
        B = unpack('f'*1023183, F.read(4*1023183))
        A = np.array(B).reshape(1043, 981)
        F.close()
    
        #a = something column 1   # some further processing
        #b =  something column 2  # and generates 1D data
    
    
        ax2 =fig.add_subplot(212,axisbg='w')
        ax2.plot(a,b)
    
    plt.show()
    

