首页 > 解决方案 > 如何在python中为多个图形制作动画

问题描述

-------------------- 总结 ------------

我想使用 FuncAnimation 为 3+ 个图形制作动画,方法是使用相同的动画对象来加速代码。

实现这一目标的最佳方法是什么?我做错了什么,因为我的使用return a + b + c不起作用,因为这是 + 的未报告操作数类型?

-------------------- 总结 ------------

我正在做一个项目,如果我需要实时打印数据,因为数据分布在不同的测量值上,我是否需要将它们分成不同的图表,既便于使用,又因为它们的幅度变化很大。

有很多例子是如何为一张图制作动画,而不是如何为几张图制作动画。我发现 如何使用 FuncAnimation 使用 matplotlib 更新和动画多个图形? https://www.reddit.com/r/learnpython/comments/4a0e8v/live_graphing_multiple_subplots_with_matplotlib/

我最成功的一个是在一个图形中创建多个子图并为该图形设置动画。我已经根据上面提到的第一个示例设置了以下测试程序。

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

x1 = np.arange(130, 190, 1)
x2 = np.arange(130, 190, 1)
x3 = np.arange(130, 190, 1)
test1 = 1 * np.sin(2 * np.pi * x1 / 10)
test2 = 2 * np.sin(2 * np.pi * x2 / 10)
test3 = 3 * np.sin(2 * np.pi * x3 / 10)

xa, xb, xc, y1, y2, y3 = [], [], [], [], [], []

fig = plt.figure(1)
ax1 = fig.add_subplot(131)
line1, = ax1.plot([], [])
ax1.set_xlabel('Samples')
ax1.set_ylabel('Amplitude')
ax1.set_title('Line 1')

ax2 = fig.add_subplot(132)
line2, = ax2.plot([], [])
ax2.grid(True)
ax2.set_xlabel('Samples')
ax2.set_ylabel('Amplitude')
ax2.set_title('Line 2')

ax3 = fig.add_subplot(133)
line3, = ax3.plot([], [])
ax3.grid(True)
ax3.set_xlabel('Samples')
ax3.set_ylabel('Amplitude')
ax3.set_title('Line 3')


def update_line_1(i):
    xa.append(x1[i])
    y1.append(test1[i])
    line1.set_data(xa, y1)
    return line1


def update_line_2(i):
    xb.append(x2[i])
    y2.append(test2[i])
    line2.set_data(x2, y2)
    return line2


def update_line_3(i):
    xc.append(x3[i])
    y3.append(test3[i])
    line3.set_data(x3, y3)
    return line3


def update_all(i):
    a = update_line_1(i)
    b = update_line_2(i)
    c = update_line_3(i)
    return a + b + c


animALL = FuncAnimation(fig, update_all, interval=50, blit=True)
plt.show()

但是使用return a + b + cdo 不起作用,因为这是 + 的未报告操作数类型

TypeError: unsupported operand type(s) for +: 'Line2D' and 'Line2D'

由于我对动画比较陌生,如果这是正确的方法,并且知识渊博的社区确实知道更好的方法,或者你们中的一个人可能会帮助我理解我在为多个子图添加更新信息时做错了什么,我是否会感到困惑。

标签: pythontypeerrorsubplot

解决方案


能够自己单独使用它,我的问题是我尝试返回一个线对象以FuncAnimation供使用,而不是在更新函数内部绘制图形。

如果将来有人遇到同样的问题,我会留下我的代码。

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


def my_function(i):

    sin = np.sin(2 * np.pi * (i + 1) / 10)

    # Changing the data for line 1
    line1.pop(0)    # Removing the first element
    line1.append(1 * sin)      # Adding the last element

    # Changing the data for line 2
    line2.pop(0)  # Removing the first element
    line2.append(2 * sin)  # Adding the last element

    # Changing the data for line 3
    line3.pop(0)  # Removing the first element
    line3.append(3 * sin)  # Adding the last element

    # Changing the data for line 4
    line4.pop(0)  # Removing the first element
    line4.append(4 * sin)  # Adding the last element

    # Changing the data for line 5
    line5.pop(0)  # Removing the first element
    line5.append(5 * sin)  # Adding the last element

    # Changing the data for line 6
    line6.pop(0)  # Removing the first element
    line6.append(6 * sin)  # Adding the last element

    # Changing the data for sample
    sample.pop(0)  # Removing the first element
    sample.append((i + 1) / 10)  # Adding the last element

    # clear axis
    ax1.clear()
    ax2.clear()

    # plot the graphs for the first subgraph
    ax1.plot(sample, line1, label='Joint 1')
    ax1.plot(sample, line2, label='Joint 2')
    ax1.plot(sample, line3, label='Joint 3')

    # plot the graphs for the second subgraph
    ax2.plot(sample, line4, label='Joint 4')
    ax2.plot(sample, line5, label='Joint 5')
    ax2.plot(sample, line6, label='Joint 6')

    # Add legend, labels and titles to subgraph 1
    ax1.legend(['Joint 1', 'Joint 2', 'Joint 3'])
    ax1.set_xlabel('Samples')
    ax1.set_ylabel('Torque (Nm)')
    ax1.set_title('Subgraph 1')

    # Add legend, labels and titles to subgraph 2
    ax2.legend(['Joint 4', 'Joint 5', 'Joint 6'])
    ax2.set_xlabel('Samples')
    ax2.set_ylabel('Torque (Nm)')
    ax2.set_title('Subgraph 2')


# start collections with zeros
line1 = [0] * 10
line2 = [0] * 10
line3 = [0] * 10
line4 = [0] * 10
line5 = [0] * 10
line6 = [0] * 10
sample = [0] * 10

# define and adjust figure
fig = plt.figure(figsize=(12, 6))
ax1 = plt.subplot(121)
ax2 = plt.subplot(122)

# animate
ani = animation.FuncAnimation(fig, my_function, interval=100)
plt.show()

推荐阅读