Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.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_Animation_Matplotlib - Fatal编程技术网

在python中绘制两点(一点正在移动)之间的动画线

在python中绘制两点(一点正在移动)之间的动画线,python,animation,matplotlib,Python,Animation,Matplotlib,这里有一个固定点和一个可变点,每次迭代都会改变他的位置。我想在每次迭代中在这两点之间画一条线,就像有一条线在改变他的梯度一样 以下是这两点的代码: import matplotlib.pyplot as plt import numpy as np from matplotlib.animation import FuncAnimation list_var_points = (1, 5, 4, 9, 8, 2, 6, 5, 2, 1, 9, 7, 10) fig, ax = plt.sub

这里有一个固定点和一个可变点,每次迭代都会改变他的位置。我想在每次迭代中在这两点之间画一条线,就像有一条线在改变他的梯度一样

以下是这两点的代码:

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

list_var_points = (1, 5, 4, 9, 8, 2, 6, 5, 2, 1, 9, 7, 10)

fig, ax = plt.subplots()
xfixdata, yfixdata = [14], [8]
xdata, ydata = [5], []
ln, = plt.plot([], [], 'ro', animated=True)
plt.plot([xfixdata], [yfixdata], 'bo')

def init():
    ax.set_xlim(0, 15)
    ax.set_ylim(0, 15)
    return ln,

def update(frame):
    ydata = list_var_points[frame]
    ln.set_data(xdata, ydata)
    return ln,          


ani = FuncAnimation(fig, update, frames=range(len(list_var_points)),
            init_func=init, blit=True)
plt.show()

谢谢大家!

动画线可以采用2个点,而不是仅采用一个点,这样两个点都与一条线连接

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

list_var_points = (1, 5, 4, 9, 8, 2, 6, 5, 2, 1, 9, 7, 10)

fig, ax = plt.subplots()
xfixdata, yfixdata = 14, 8
xdata, ydata = 5, None
ln, = plt.plot([], [], 'ro-', animated=True)
plt.plot([xfixdata], [yfixdata], 'bo', ms=10)

def init():
    ax.set_xlim(0, 15)
    ax.set_ylim(0, 15)
    return ln,

def update(frame):
    ydata = list_var_points[frame]
    ln.set_data([xfixdata,xdata], [yfixdata,ydata])
    return ln,          


ani = FuncAnimation(fig, update, frames=range(len(list_var_points)),
            init_func=init, blit=True)
plt.show()

动画线条可以包含两个点,而不是一个点,这样两个点都可以用一条线连接

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

list_var_points = (1, 5, 4, 9, 8, 2, 6, 5, 2, 1, 9, 7, 10)

fig, ax = plt.subplots()
xfixdata, yfixdata = 14, 8
xdata, ydata = 5, None
ln, = plt.plot([], [], 'ro-', animated=True)
plt.plot([xfixdata], [yfixdata], 'bo', ms=10)

def init():
    ax.set_xlim(0, 15)
    ax.set_ylim(0, 15)
    return ln,

def update(frame):
    ydata = list_var_points[frame]
    ln.set_data([xfixdata,xdata], [yfixdata,ydata])
    return ln,          


ani = FuncAnimation(fig, update, frames=range(len(list_var_points)),
            init_func=init, blit=True)
plt.show()

连接这两点的线?有什么问题?连接这两点的线?有什么问题?非常感谢!我用一种错误的方式来处理这个问题。我还得到了错误“ValueError:settingarray element with a sequence”,我想这是点的值中的[](brakets)。非常感谢!我用一种错误的方式来处理这个问题。我还得到了错误“ValueError:settingarray element with a sequence”,我想这是点的值中的[](brakets)。