我正在尝试在东北框架中绘制几艘不同船只的运动动画。每艘船都由圆形标记、颜色和 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()