使用 imshow() 和 matplotlib.patches 在动态图像中快速绘制静态圆圈
Fast Static Circles in a Dynamic Image using imshow() and matplotlib.patches
为了提出这个问题,我清理了我的代码,然后在清理的过程中找到了解决方案。请参阅下面我的解决方案。
我正在尝试在 imshow()
中创建动态图像(电影),并使用 matplotlib.patches
在其顶部绘制静态圆圈,但随着电影的播放速度变慢(延迟随时间线性增加)。圆圈是静态的,因此必须有一种方法可以使 matplotlib.patches
运行 更快,因为 imshow()
正在更新。这是我的代码:
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()
事实证明这是一个非常简单的解决方案。 add_patch()
函数只需要在影片开头运行一次,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()
这 运行 秒的时间几乎不变。希望这会在将来为其他人节省几个小时的调试时间。
为了提出这个问题,我清理了我的代码,然后在清理的过程中找到了解决方案。请参阅下面我的解决方案。
我正在尝试在 imshow()
中创建动态图像(电影),并使用 matplotlib.patches
在其顶部绘制静态圆圈,但随着电影的播放速度变慢(延迟随时间线性增加)。圆圈是静态的,因此必须有一种方法可以使 matplotlib.patches
运行 更快,因为 imshow()
正在更新。这是我的代码:
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()
事实证明这是一个非常简单的解决方案。 add_patch()
函数只需要在影片开头运行一次,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()
这 运行 秒的时间几乎不变。希望这会在将来为其他人节省几个小时的调试时间。