2017-05-08 10 views
0

この問題を尋ねるためにコードを整理し、解決している間に解決策を見つけました。以下の私の解決策を見てください。imshow()とmatplotlib.patchesを使用したダイナミックイメージの高速静的円

matplotlib.patchesを使用して静的な円を描いたimshow()の動画(動画)を作成しようとしていますが、ムービーの再生速度が遅くなります(待ち時間は時間とともに直線的に増加しています)。サークルは静的なので、imshow()が更新されているため、matplotlib.patchesを速く実行させる方法が必要です。ここに私のコードです:

import matplotlib.pyplot as plt 
import numpy as np 
from matplotlib.patches import Circle 
from scipy.linalg import toeplitz 

# Radius for circle of circles 
r = 0.5 
# Number of circles 
n = 7 
# Locations of centers of circles 
a = r*np.transpose(np.array([np.cos(np.arange(0,2*np.pi,2*np.pi/n)), 
          np.sin(np.arange(0,2*np.pi,2*np.pi/n))])) 

# Create first background image. 
E = toeplitz(np.random.rand(70)) 

# Plot the first frame. 
fig = plt.figure(1) 
ax = fig.add_subplot(111) 
im = ax.imshow(E,extent=np.array([-1,1,-1,1])) 
# Draw the circles on the image 
for k in range(np.shape(a)[0]): 
    ax.add_patch(Circle((a[k][0],a[k][1]),0.1)) 
plt.show() 

# Update with background image and redraw the circles. 
for t in range(60): 
    # Update the background image. 
    E=toeplitz(np.random.rand(70)) 
    im.set_array(E) 
    # Update the circles 
    for k in range(np.shape(a)[0]): 
     ax.add_patch(Circle((a[k][0],a[k][1]),0.1)) 
    fig.canvas.draw() 

答えて

0

これは非常に簡単な解決策であることが判明しました。 add_patch()関数は、ムービーの最初に1回だけ実行する必要があり、set_arrayは円の後ろの配列を更新します。ここでは、メインforループから削除add_patch()機能付きのコードは次のとおりです。

import matplotlib.pyplot as plt 
import numpy as np 
from matplotlib.patches import Circle 
from scipy.linalg import toeplitz 

# Radius for circle of circles 
r = 0.5 
# Number of circles 
n = 7 
# Locations of centers of circles 
a = r*np.transpose(np.array([np.cos(np.arange(0,2*np.pi,2*np.pi/n)), 
          np.sin(np.arange(0,2*np.pi,2*np.pi/n))])) 

# Create first background image. 
E = toeplitz(np.random.rand(70)) 

# Plot the first frame. 
fig = plt.figure(1) 
ax = fig.add_subplot(111) 
im = ax.imshow(E,extent=np.array([-1,1,-1,1])) 
# Draw the circles on the image 
for k in range(np.shape(a)[0]): 
    ax.add_patch(Circle((a[k][0],a[k][1]),0.1)) 
plt.show() 

# Update with background image. 
for t in range(60): 
    E=toeplitz(np.random.rand(70)) 
    im.set_array(E) 
    fig.canvas.draw() 

これは、ほぼ一定の時間で実行されます。うまくいけば、将来他の誰かが数時間のデバッグをするのを避けることができます。

関連する問題