python - matplotlib 中的动画箭头
问题描述
我在 matploltib 中对一行进行了动画处理,代码的输出如下所示:
但我想要的是代码应该绘制一个箭头(即行尾的箭头)而不是这一行,这是代码片段:
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()
动画片
推荐阅读
- python - tflite_runtime 有时会在我的 Coral Dev Board Mini 上冻结
- javascript - Redux 数据更新不分派
- python - 如何在 python 中使用 xarray 将气候数据集相乘?
- python - ML 笔记本中的 Azure Batch API 上升“AttributeError”
- javascript - 将 CSS 应用于通过 JavaScript 加载的 iframe
- python - cli中的python双制表符仅显示兼容文件
- spring - Hazelcast 嵌入式缓存打印太多日志(目标是此节点!-> [10.1.8.58]:5701","stack_trace":"<#d3566be0> jlIllegalArgumentException...)
- mysql - 解决导致数据库被阻塞的 MySQL 连接问题
- sqlite - 未找到 Apache Airflow 提供程序 Sqlite 模块/钩子“airflow.hooks.dbapi import DbApiHook”错误
- javascript - 看到错误 - “[match] query malformed, no start_object after query name”不知道为什么