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

使用matplotlib的动画子图

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

使用matplotlib的动画子图

基本上,您可以使用与示例中的结构非常相似的结构。您只需要创建一个附加轴(子图)和第二条线对象:

import numpy as npimport matplotlib.pyplot as pltimport matplotlib.animation as animationdef data_gen():    t = data_gen.t    cnt = 0    while cnt < 1000:        cnt+=1        t += 0.05        y1 = np.sin(2*np.pi*t) * np.exp(-t/10.)        y2 = np.cos(2*np.pi*t) * np.exp(-t/10.)        # adapted the data generator to yield both sin and cos        yield t, y1, y2data_gen.t = 0# create a figure with two subplotsfig, (ax1, ax2) = plt.subplots(2,1)# intialize two line objects (one in each axes)line1, = ax1.plot([], [], lw=2)line2, = ax2.plot([], [], lw=2, color='r')line = [line1, line2]# the same axes initalizations as before (just now we do it for both of them)for ax in [ax1, ax2]:    ax.set_ylim(-1.1, 1.1)    ax.set_xlim(0, 5)    ax.grid()# initialize the data arrays xdata, y1data, y2data = [], [], []def run(data):    # update the data    t, y1, y2 = data    xdata.append(t)    y1data.append(y1)    y2data.append(y2)    # axis limits checking. Same as before, just for both axes    for ax in [ax1, ax2]:        xmin, xmax = ax.get_xlim()        if t >= xmax: ax.set_xlim(xmin, 2*xmax) ax.figure.canvas.draw()    # update the data of both line objects    line[0].set_data(xdata, y1data)    line[1].set_data(xdata, y2data)    return lineani = animation.FuncAnimation(fig, run, data_gen, blit=True, interval=10,    repeat=False)plt.show()


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

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

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