如何为收敛的迭代制作动画

时间:2019-02-26 13:21:41

标签: python python-3.x loops animation python-3.6

我正在尝试对一维Gerchberg-Saxton算法的多次迭代进行收敛动画,但是还没有设法使用matplotlib.animation或drawow使它起作用。我可以在下面的代码中使用for循环将它们全部同时绘制在一个图形上,但是希望依次看到它们。有人可以帮忙吗?

import numpy as np
import pyfftw.interfaces.numpy_fft as fft
from matplotlib import pyplot as plt

aperture_width = 10
constraint_factor = 11
num_points = 2**16 
aperture_x = np.linspace(-20*aperture_width, 20*aperture_width, num_points)

# object to be recovered
g = np.array( abs(aperture_x) < (aperture_width/2), dtype=np.complex128)

# Field at Image plane
G = fft.fftshift(fft.fft(fft.fftshift(g)))

#Intensity at Image plane
G = abs(G)**2     # Only known value

constraint = np.array( abs(aperture_x) < (constraint_factor/2))  #object constraint - guess of object size

Gp = np.copy(G)  # make a copy of G

for i in range(10):

    gp = fft.fftshift(fft.ifft(fft.fftshift(np.sqrt(Gp))))     # first object "guess"
    gp = gp*constraint                                        # apply constraint to object

    Gp= fft.fftshift(fft.fft(fft.fftshift(gp)))              # calculate new image plane
    Gp = np.sqrt(G)*np.exp(1j*np.angle(Gp))                     # apply image domain constraint

    plt.plot(aperture_x,abs(G)**2/max(abs(G)**2),aperture_x,abs(Gp)**2/max(abs(Gp)**2))
    plt.xlim(-3,3)
    plt.pause(0.05)
plt.show()

1 个答案:

答案 0 :(得分:0)

绘制动画使用情况 plt.pause(0.05) #0.05 is time in secs 在地块之间。 这是示例代码

import matplotlib as plt

listValues = [i for i in range(0,10)] #random list just to show the basic concept

figure = plt.figure() #initialise figure object outside the loop
ax = figure.add_subplot(1,1,1) # get the subplot
for idx in listValues:
    ax.scatter(idx,idx) # here is your plotting
    plt.pause(0.05)

plt.show()