Python Forum
closing image automatically
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
closing image automatically
#1
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.

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()
Moderator Larz60+: Added Python tags. Please do this in the future (see help, BBCODE)
Reply
#2
For displaying image without blocking (so you can do something in "background" and at the end close plot with .close())  its possible to use plt.pause(), following code displays consequently five plots, each for 3 seconds.
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 100)

for i in range(5):
    plt.plot(x, np.power(x, i))
    plt.pause(3)
    plt.close()
And if you need only saved images, you dont need to show image at all, .savefig() alone works.

There is matplotlib.animation module, it can be used to produce "movies".
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to automatically align an image robrod26 1 2,025 May-09-2020, 04:04 PM
Last Post: ThomasL

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020