python - matplotlib 中的动画箭头

标签 python matplotlib animation vector

我在 matploltib 中对一行进行了动画处理,代码的输出如下所示:

enter image description here

但是我想要的是代码应该绘制一个箭头(即行尾的箭头),而不是这一行,这里是代码片段:

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

fig, ax = plt.subplots(figsize=(12, 8))
ax.set(xlim=(0, 104), ylim=(0, 68))

x_start, y_start = (50, 35)
x_end, y_end = (90, 45)

x = np.linspace(x_start, x_end, 50)
y = np.linspace(y_start, y_end, 50)

line, = ax.plot(x, y)

def animate(i):
    line.set_data(x[:i], y[:i])
    return line,


ani = animation.FuncAnimation(
    fig, animate, interval=20, blit=True, save_count=50)


plt.show()

我应该在代码中添加/更改什么,以便在输出中获得箭头而不是行?

最佳答案

回答

您可以使用ax.arrow绘制箭头。
请注意,您应该使用 ax.cla() 清除绘图,并在每次迭代时使用 ax.set() 调整轴限制。

代码

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

fig, ax = plt.subplots(figsize=(12, 8))
ax.set(xlim=(0, 104), ylim=(0, 68))

x_start, y_start = (50, 35)
x_end, y_end = (90, 45)

N = 50
x = np.linspace(x_start, x_end, N)
y = np.linspace(y_start, y_end, N)

def animate(i):
    ax.cla()
    ax.arrow(x_start, y_start,
             x[i] - x_start, y[i] - y_start,
             head_width = 2, head_length = 2, fc = 'black', ec = 'black')
    ax.set(xlim = (0, 104), ylim = (0, 68))

ani = animation.FuncAnimation(fig, animate, interval=20, frames=N, blit=False, save_count=50)

plt.show()

动画

enter image description here

关于python - matplotlib 中的动画箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63914377/

相关文章:

javascript - Angular JS 中的动画检查元素是否正在补间 greensock

python - 创建具有相似拼写的 pandas 数据框变量名称列表

python - Django-获取外键对象列表

python - 绘制直方图,使总高度等于 1

python - 如何更改图例中散点标记的大小?

css - 为什么这个 CSS3 旋转动画设置在 Chrome 和 Safari 上不起作用?

python - 为什么在更改其值时通过函数中的索引访问列表项有效,但迭代器变量方式却不起作用?

python - 值错误 : axes don't match array - Can't transpose an array

python - 检索 Matplotlib 热图颜色

ANDROID - 逐帧动画