仅根据情节中的更新颜色进行动画处理 [英] Animation based on only updating colours in a plot

查看:62
本文介绍了仅根据情节中的更新颜色进行动画处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由大量线组成的图.在动画的每一步中,线条的颜色都应该更新,但是在线条上进行for循环似乎确实很昂贵.有没有更好的方法可以做到这一点?

I have a plot which consists of great number of lines. At each step the colours of lines should get updated in the animation, but doing a for loop on lines seems to be really costly. Is there any better way to do that?

这是我的代码:

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

此外,我无法与动画艺术家合作!我用平局.动画行有什么问题

Plus I couldn't make it to work with animation artist! I used draw. What is wrong with the animation lines

现在将那些10s增加到100会使程序非常慢:

Now increasing those 10s to 100 makes the program terribly slow:

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

正如我所说,我想与动画并排运行.因此,我更喜欢将其制作成动画.我认为这至少可以在动画开始后解决滞后问题,但是现在按照我的定义,它是行不通的.

As I said I want to run it side by side with an animation. Therefore I prefer to make it an animation. I think that would solve the lagging problem at least after the animation starts but right now the way I defined it, it doesn't work.

推荐答案

为此最容易使用LineCollection.这样,您可以将所有颜色设置为单个阵列,并且通常可以获得更好的绘图性能.

It's easiest to use a LineCollection for this. That way you can set all of the colors as a single array and generally get much better drawing performance.

更好的性能主要是因为集合是在matplotlib中绘制许多相似对象的一种优化方法.在这种情况下,避免嵌套循环来设置颜色实际上是次要的.

The better performance is mostly because collections are an optimized way to draw lots of similar objects in matplotlib. Avoiding the nested loops to set the colors is actually secondary in this case.

考虑到这一点,请尝试以下方法:

With that in mind, try something more along these lines:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.collections import LineCollection
import matplotlib.animation as animation

lines=[]
for i in range(10):
    for j in range(10):
        lines.append([(0, i), (1, j)])

fig, ax = plt.subplots()
colors = np.random.random(len(lines))
col = LineCollection(lines, array=colors, cmap=plt.cm.gray, norm=plt.Normalize(0,1))
ax.add_collection(col)
ax.autoscale()

def update(i):
    colors = np.random.random(len(lines))
    col.set_array(colors)
    return col,

# Setting this to a very short update interval to show rapid drawing.
# 25ms would be more reasonable than 1ms.
ani = animation.FuncAnimation(fig, update, interval=1, blit=True, 
                              init_func=lambda: [col])
# Some matplotlib versions explictly need an `init_func` to display properly...
# Ideally we'd fully initialize the plot inside it. For simplicitly, we'll just
# return the artist so that `FuncAnimation` knows what to draw.
plt.show()

这篇关于仅根据情节中的更新颜色进行动画处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆