【发布时间】:2013-12-01 23:13:44
【问题描述】:
我有一个包含大量线条的情节。在每一步,线条的颜色都应该在动画中更新,但是在线条上做一个 for 循环似乎真的很昂贵。有没有更好的方法来做到这一点?
这是我的代码:
import numpy as np
lines=[]
from matplotlib import pyplot as plt
import matplotlib.animation as animation
#initial plot
fig=plt.figure()
ax=plt.subplot(1,1,1)
for i in range(10):
lines.append([])
for j in range(10):
lines[i].append(ax.plot([i,j],color='0.8'))
lines=np.asarray(lines)
##Updating the colors 10 times
im=[]
for steps in range(10):
colors=np.random.random(size=(10,10))
for i in range(10):
for j in range(10):
lines[i,j][0].set_color(str(colors[i,j]))
plt.draw()
# im.append(ax)
plt.pause(.1)
#ani = animation.ArtistAnimation(fig, im, interval=1000, blit=True,repeat_delay=1000)
plt.show()
另外,我无法与动画艺术家合作!我用画法。动画线条有什么问题
现在将这 10 个增加到 100 个会使程序变得非常缓慢:
import numpy as np
lines=[]
from matplotlib import pyplot as plt
import matplotlib.animation as animation
#initial plot
fig=plt.figure()
ax=plt.subplot(1,1,1)
for i in range(100):
lines.append([])
for j in range(100):
lines[i].append(ax.plot([i,j],color='0.8'))
lines=np.asarray(lines)
##Updating the colors 10 times
im=[]
for steps in range(10):
colors=np.random.random(size=(100,100))
for i in range(100):
for j in range(100):
lines[i,j][0].set_color(str(colors[i,j]))
plt.draw()
# im.append(ax)
plt.pause(.1)
#ani = animation.ArtistAnimation(fig, im, interval=1000, blit=True,repeat_delay=1000)
plt.show()
正如我所说,我想将它与动画并排运行。所以我更喜欢把它做成动画。我认为这至少可以在动画开始后解决滞后问题,但现在我定义它的方式,它不起作用。
【问题讨论】:
-
如果有任何不清楚的地方,我很乐意澄清。我已经从头开始重写了这个例子,只是为了找到做我想做的事情的最佳方法。
-
我不想问这个,但对你来说什么是慢的。我运行了你展示的整个代码,它在大约 5 秒内显示了一系列漂亮的图。
-
@Paul,当然,我编辑以澄清你的抱怨。
标签: python numpy matplotlib plot