matplotlib - 多对象运动的动画图 - python
问题描述
我正在尝试在东北框架中绘制几艘不同船只的运动动画。每艘船都由圆形标记、颜色和 ID 标识。血管信息存储在对象列表中,Vessel
每个对象具有positions
存储north
和east
坐标的属性。
我只想保留最新船的位置,所以应该清除所有早期的点。
到目前为止,我所写的内容绘制了点,但我找不到指定其他参数(例如名称和颜色参数)的方法。我宁愿不必line
为每个容器创建单独的对象,因为 ID 不一定是排序的,并且将来可能需要存储在单独列表中的静态对象。
我到目前为止的代码是
def plot_animation(vessels, objects, ts=10, n_min=3, **kwargs):
import matplotlib.animation as animation
"""Plots the results of a simulation
input:
vessels: List of `Vessel()` type
n_min: The number of minutes between each position scatter point
**kwargs: Keyword arguments for the plotting commands
output:
None
"""
pos_marker = kwargs.get("pos_marker", "o") # options
pos_marker_size = kwargs.get("pos_marker_size", 50)
frames = len(vessels[0].get_positions()) # number of simulation time-points
axis_min = 0
axis_max = 0
for v in vessels: # for determining axis bounds
positions = v.get_positions()
axis_min = min(min([min(pos) for pos in positions]), axis_min)
axis_max = max(max([max(pos) for pos in positions]), axis_max)
fig = plt.figure()
ax = fig.add_subplot(
111, autoscale_on=False, xlim=(axis_min, axis_max), ylim=(axis_min, axis_max)
)
ax.set_aspect("equal")
ax.grid()
ax.set_xlabel("East [km]")
ax.set_ylabel("North [km]")
(line,) = ax.plot([], [], "o")
def init():
line.set_data([], [])
return (line,)
def animate(i):
x = []
y = []
for v in vessels:
positions = v.get_positions()
north = positions[i].north
east = positions[i].east
x.append(east)
y.append(north)
line.set_data(x, y)
return (line,)
ani = animation.FuncAnimation(
fig, animate, frames=frames, init_func=init, blit=True
)
plt.show()
解决方案
推荐阅读
- vue.js - ECharts - 如何从道具中注入价值?
- ios - 在 resigned ipa 中注入 ObjectiveC-Swift 钩子,主要在 Swift 中而不是在 Objective C 中编写钩子
- html - 根据滚动位置触发不同的 CSS 关键帧动画(我应该做一个动画类吗?)
- html - 如何使 HTML 字段集更紧密?
- python - 当主窗口最小化时,为什么顶层窗口会被破坏?
- arrays - 带有指针数组的函数参数中的静态关键字?
- hyperlink - achor 标记上的 Onclick 标记不再起作用?
- java - 操作系统之间的Java性能差异太大
- excel - 如何避免选择隐藏和取消隐藏行
- android - 电子邮件意图无法附加附件