closing image automatically in for loop , python

William Ray Wing wrw at mac.com
Thu Apr 13 00:04:59 EDT 2017


> On Apr 12, 2017, at 7:18 AM, Masoud Afshari <mafshari44 at gmail.com> wrote:
> 
> Dear all
> 
> I have several *.sfd files which created by a simulation code. I wrote a program containing a for Loop which reads  each time one .sfd file  and plot the requested Parameters. I have two request:
> 
> 1- my Problem is that for Showing successive Images in for Loop I have to Close the Image MANAULLY each time to read next sdf file. can anyone please tell me which command do I have to use so the code Close the Images automatically.
> 

I normally run matplolib in interactive mode, so this may not be quite correct, BUT I think you only need to add a call to:

close()

After your call to plt.show()

Bill


> 2- more over, can anyone please tell me how can I create a movie with this code.
> 
> in the following you can see my code
> 
> 
> #####################
> # in this program, PYTHON reads the reduced files and plot the variables .
>  
>  
> import sys 
> import sdf
> import numpy as np
> import matplotlib.pyplot as plt 
> from matplotlib.font_manager import FontProperties
> fp = FontProperties('Symbola')
>  
> ##################### information from EPOCH input.deck
> nx,ny= 1200, 1600
>  
> xmin=-100e-6
> xmax = 50e-6
> ymin = -100e-6
> ymax = 100e-6
>  
> X =np.linspace(xmin,xmax,nx) #Generate linearly spaced vector. The spacing between the points is (x2-x1)/(n-1).
> Y =np.linspace(ymin,ymax,ny)
>  
> #################
> for n in range(0,27):
> nstr = str(n)#.zfill(4)
> #print nstr
>  
> ######
> fig = plt.figure() #plot several figures simultaneously 
> 
> ########..... reading Ex
>  
> filename ="Ex_resample" +'_sdf_'+ str(n)+'.dat'
> with open(filename, 'rb') as f: #read binary file
> data = np.fromfile(f, dtype='float64', count=nx*ny) #float64 for Double precision float numbers
> Ex = np.reshape(data, [ny, nx], order='F')
> #print Ex.max()
>  
> #Display image with scaled colors:
> plt.subplot(222)
> fig1=plt.imshow(Ex, extent=[X.min()*1e6, X.max()*1e6, Y.min()*1e6, Y.max()*1e6], vmin=0, vmax=2e12, cmap='brg', aspect='auto') #cmap='jet', 'nipy_spectral','hot','gist_ncar'
> #plt.suptitle('Ex')
> plt.title('sdf '+ str(n)+ '; Time= ' +str(n*50)+ 'ps',color='green', fontsize=15)
> plt.xlabel('x($\mu$m)')
> plt.ylabel('y($\mu$m)')
> plt.text(40,80,'Ex',color='red', fontsize=15)
> plt.colorbar()
> plt.show()
> #fig.savefig('test.jpg')
> sys.exit()
> -- 
> https://mail.python.org/mailman/listinfo/python-list


More information about the Python-list mailing list