简体   繁体   English

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

[英]Plot an animated line between two points (one is moving) in python

Here there´sa fixed point and a variable point, which changes his position every iteration. 这里有一个固定点和一个可变点,它们每次迭代都会更改其位置。 I would like to animate a line between these two points every iteration, as if there was a line changing his gradient. 我想在每次迭代中为这两个点之间的一条线设置动画,好像有一条线在改变他的渐变。

Here is the code of these two points: 这是这两点的代码:

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()

Thank you! 谢谢!

The animated line can take 2 points instead of only one, such that both points are connected with a line. 动画线可以取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()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM