对具有不同颜色的线进行动画处理 [英] Animate a line with different colors

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

问题描述

我正在尝试为演示文稿动画化我的数据.我正在尝试使用python的动画包来这样做.我试图做的事情大致可以归结为 http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/

I am in the process of trying to animate my data for a presentation. I am trying to use the animation package of python to do so. What I am trying to do boils down roughly to the first example in http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))
line, = ax.plot([], [], lw=2)

# initialization function: plot the background of each frame
def init():
    line.set_data([], [])
    return line,

# animation function.  This is called sequentially
def animate(i):
    x = np.linspace(0, 2, 1000)
    y = np.sin(2 * np.pi * (x - 0.01 * i))
    line.set_data(x, y)
    return line,

# call the animator.  blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, init_func=init,
                           frames=200, interval=20, blit=True)

我是python的新手,所以我努力了解正在发生的事情.在我看来,init()和animate(i)都不会修改它们返回的内容.而且,他们正在修改的对象(行(而不是行))以前没有声明过.

I am new to python so I'm struggling with understanding what's happening. To me, it seems neither init() nor animate(i) modify what they return. Moreover, the object they're modifying, line (and not line,) hasn't been declared previously.

无论哪种方式,我想要做的就是获取数据,在这种情况下为正弦波,逐段着色.蓝色介于0和1之间,蓝色介于1和1.5之间,蓝色再次介于1.5和2之间.我已经尝试了很多方法,但是无法使其正常工作.我尝试使用这些函数返回整个图形,而不仅仅是返回线,希望它们刷新预览图形并绘制我绘制的复合线,但无济于事.

Either way, what I am trying to do is to have the data, in this case the sine wave, piece-wise colored. Blue between 0 and 1, red between 1 and 1.5, and blue again between 1.5 and 2. I have tried many things, but can't get this to work. I tried for those functions to return the entire figure, not only the line, hoping that they would flush the previewing figure and plot the composite line I drew, to no avail.

在这个框架中,我如何为由线(不同颜色)组成的线设置动画?

How can I, in this framework, animate a line that is made up of bits of lines (of different colors) ?

推荐答案

要绘制其属性沿其变化的线,请使用LineCollection.示例此处例子.

To plot a line whose properties change along it, use a LineCollection. Example here and here. To make an animation with a LineCollection, see this example.

要回答事物在上面的代码中的工作方式,请在animate()中用line.set_data(x, y)重置行x,y属性.然后该函数将Matplotlib必须在每一帧更新的艺术家集合(line,)返回给Matplotlib.

To answer how the thing works in the code above, within animate() you are resetting the line x,y properties with line.set_data(x, y). the function then returns to Matplotlib the set (line,) of artists that matplotlib must update at each frame.

这就是我在寻找的内容(仅当您的平台在上次通话中支持blit=true时才启用它):

Here's what I think you're looking for (enable blit=true only if your platform supports it in the last call):

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
from matplotlib.collections import LineCollection
from matplotlib.colors import ListedColormap, BoundaryNorm

# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))

cmap = ListedColormap(['b', 'r', 'b'])
norm = BoundaryNorm([0, 1, 1.5, 2], cmap.N)

line = LineCollection([], cmap=cmap, norm=norm,lw=2)
line.set_array(np.linspace(0, 2, 1000))
ax.add_collection(line)

# initialization function: plot the background of each frame
def init():
    line.set_segments([])
    return line,

# animation function.  This is called sequentially
def animate(i):
    x = np.linspace(0, 2, 1000)
    y = np.sin(2 * np.pi * (x - 0.01 * i))
    points = np.array([x, y]).T.reshape(-1, 1, 2)
    segments = np.concatenate([points[:-1], points[1:]], axis=1)
    line.set_segments(segments)
    return line,

# call the animator.  blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, init_func=init,
                           frames=200, interval=20)

这篇关于对具有不同颜色的线进行动画处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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