2017-05-08 1 views
0

이 문제를 묻기 위해 코드를 정리 한 다음 정리하는 동안 해결책을 찾았습니다. 아래 내 솔루션을 참조하십시오.imshow() 및 matplotlib.patches를 사용하여 동적 이미지의 빠른 정적 서클

나는 동적 영상 (동영상)을 matplotlib.patches을 사용하여 그 위에 그려진 정적 원을 사용하여 만들려고 시도하고 있지만 동영상이 재생 될 때 속도가 느려집니다 (대기 시간은 시간에 따라 선형 적으로 증가합니다). 서클은 고정되어 있으므로 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() 함수는 동영상 시작 부분에서 한 번만 실행하면되고 set_array은 서클 뒤에 배열을 업데이트합니다.

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() 

이 거의 일정한 시간으로 실행 : 여기 메인 for 루프로부터 제거 add_patch() 기능 코드이다. 바라건대 이것은 다른 누군가에게 장래에 몇 시간의 디버깅 시간을 절약 할 수 있기를 바랍니다.