【问题标题】:animated subplots using matplotlib使用 matplotlib 的动画子图
【发布时间】:2015-07-02 03:31:28
【问题描述】:

我有这个代码。我想添加一个子图来绘制余弦函数。 (我不想创建一个类)。第二个图也应该动态更新

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

def data_gen():
    t = data_gen.t
    cnt = 0
    while cnt < 1000:
        cnt+=1
        t += 0.05
        yield t, np.sin(2*np.pi*t) * np.exp(-t/10.)
data_gen.t = 0

fig, ax = plt.subplots()
line, = ax.plot([], [], lw=2)
ax.set_ylim(-1.1, 1.1)
ax.set_xlim(0, 5)
ax.grid()
xdata, ydata = [], []
def run(data):
    # update the data
    t,y = data
    xdata.append(t)
    ydata.append(y)
    xmin, xmax = ax.get_xlim()

    if t >= xmax:
        ax.set_xlim(xmin, 2*xmax)
        ax.figure.canvas.draw()
    line.set_data(xdata, ydata)

    return line,

ani = animation.FuncAnimation(fig, run, data_gen, blit=True, interval=10,
    repeat=False)
plt.show()

【问题讨论】:

    标签: python matplotlib animation


    【解决方案1】:

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

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.animation as animation
    
    def 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, y2
    
    data_gen.t = 0
    
    # create a figure with two subplots
    fig, (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 line
    
    ani = animation.FuncAnimation(fig, run, data_gen, blit=True, interval=10,
        repeat=False)
    plt.show()
    

    【讨论】:

    • 能否解释一下line1, = ax1.plot([], [], lw=2) 和line1 = ax1.plot([], [], lw=2) 的区别(第二版本缺少逗号)?
    • ax1.plot 返回(可能)多行的列表。使用逗号,该列表的第一个元素被分配给line1。没有逗号 line1 将包含整个列表。
    • 如果 y1 和 y2 需要在同一个子图上绘制,但是 data_gen 是一个带有一些输入参数 k 的函数,如何修改这个。所以每个子图都显示了不同的 k 值。
    • 如何在 jupyter notebook 中运行?添加%matplotlib inline 不起作用
    • @MattS %matplotlib notebook
    猜你喜欢
    • 2016-02-01
    • 1970-01-01
    • 2018-01-08
    • 1970-01-01
    • 2021-01-09
    • 2018-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多