栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

设置不同颜色的线动画

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

设置不同颜色的线动画

要绘制其属性沿其变化的线,请使用LineCollection。这里和这里的例子。要使用LineCollection制作动画,请参见以下示例。

要回答上面代码中的内容,

animate()
请在其中使用重置行
x,y
属性
line.set_data(x,y)
。然后,函数将Matplotlib
(line,)
必须在每一帧更新的艺术家集合返回给Matplotlib 。

这是我认为您要寻找的内容(

blit=true
仅当您的平台在上次调用中支持时才启用):

import numpy as npfrom matplotlib import pyplot as pltfrom matplotlib import animationfrom matplotlib.collections import LineCollectionfrom matplotlib.colors import ListedColormap, BoundaryNorm# First set up the figure, the axis, and the plot element we want to animatefig = 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 framedef init():    line.set_segments([])    return line,# animation function.  This is called sequentiallydef 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)



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/662201.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号