Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中设置多个图形的动画_Python_Typeerror_Subplot - Fatal编程技术网

如何在python中设置多个图形的动画

如何在python中设置多个图形的动画,python,typeerror,subplot,Python,Typeerror,Subplot,-------------------总和------------ 我想通过使用相同的动画对象来加速代码,使用FuncAnimation为3+个图形设置动画 实现这一目标的最佳方式是什么?当我使用返回a+b+c不起作用,因为这是+的未报告操作数类型时,我做错了什么 -------------------总和------------ 我正在做一个项目,如果我需要实时打印数据,因为数据被传输到不同的测量值上,我需要将它们分离成不同的图形,以便于使用,并且因为它们具有广泛的振幅变化 有很多例子说明了一

-------------------总和------------

我想通过使用相同的动画对象来加速代码,使用FuncAnimation为3+个图形设置动画

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

-------------------总和------------

我正在做一个项目,如果我需要实时打印数据,因为数据被传输到不同的测量值上,我需要将它们分离成不同的图形,以便于使用,并且因为它们具有广泛的振幅变化

有很多例子说明了一个人如何制作一个图形的动画,而不是如何制作多个图形的动画。我找到的那些

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

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()
但是,使用
返回a+b+c
不起作用,因为这是的未报告操作数类型+

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

由于我是动画新手,我想知道这是否是正确的方法,知识渊博的社区是否知道一种更好的方法,或者你们中的一位是否可以帮助我理解我在为多个子地块添加更新信息时犯了什么错误。

我自己能够独享它,我的问题是,我试图返回一个line对象供
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()