我正在繪制一些在 2D 空間中移動的點,我想在它們周圍添加圓圈。我有圓應該圍繞其繪制的點的所有 x、y 坐標,但我不確定如何將其添加到影片函式中。
任何幫助,將不勝感激!
import matplotlib.pyplot as plt
from launcher import sol, T, dt
from matplotlib.animation import FuncAnimation, PillowWriter
from create import p, N, walls_x, walls_y
# convert sol.y to plottable array('s) using smart slicing
def sol_convert(sol):
#T = len(sol.t) # amount of time steps
x = sol.y[0::4,:] # there are 4 values p.p. (x,y,vx,vy)
y = sol.y[1::4,:]
vx = sol.y[2::4,:]
vy = sol.y[3::4,:]
return x, y, vx ,vy
r_x, r_y, v_x, v_y = sol_convert(sol)
fig, ax = plt.subplots()
xlimit, ylimit = [-1,26], [-1,16]
axis = plt.axes(xlim =(xlimit),
ylim =(ylimit))
for i in range(len(walls_x)):
plt.plot(walls_x[i],walls_y[i],'k',linestyle="solid")
ped, = axis.plot([], [], 'o')
# what will our line dataset contain?
def init():
ax.set_xlim(xlimit)
ax.set_ylim(ylimit)
ped.set_data([], [])
return ped,
# initializing empty values for x and y co-ordinates
xdata, ydata = [], []
# animation function
def animate(frame):
# t is a parameter which varies
# with the frame number
t = dt * frame
xdata = r_x[:,frame]
ydata = r_y[:,frame]
ped.set_data(xdata, ydata)
circles = plt.Circle((r_x[:,frame],r_y[:,frame]),2,color='r', fill=False)
ax.add_patch(circles)
return ped,
anim = FuncAnimation(fig, animate,
init_func = init,
frames = T,
interval = 20,
blit = True)
# saves the animation in our desktop
writergif = PillowWriter(fps=24)
anim.save('pedestrians.gif',writer=writergif)
我試過在 animate 函式中添加代碼,但我對影片繪圖還很陌生,并不完全了解可以通過此函式提供什么型別的資料。
這是一個最小的可重現示例:
# Minimal reproducable example
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation, PillowWriter
# simulation variables
sim_time = 4 # total simulation time in [?] [is it in steps or in s]
dt = 0.5 # time step of the simulation in s
t = np.arange(0,sim_time,dt) # live running time of simulation
t_span = [t[0],t[-1]]
T = len(t) # amount of time steps for frames
fig, ax = plt.subplots()
xlimit, ylimit = [-1,26], [-1,16]
axis = plt.axes(xlim =(xlimit),
ylim =(ylimit))
points, = axis.plot([], [], 'o')
# what will our line dataset contain?
def init():
ax.set_xlim(xlimit)
ax.set_ylim(ylimit)
points.set_data([], [])
return points,
# initializing empty values for x and y co-ordinates
xdata, ydata = [], []
# animation function
def animate(frame):
# t is a parameter which varies
# with the frame number
t = dt * frame
xdata = np.random.rand(3)*10
ydata = np.random.rand(3)*10
points.set_data(xdata, ydata)
circles = plt.Circle((xdata,ydata),2,color='r', fill=False)
ax.add_patch(circles)
return points,
anim = FuncAnimation(fig, animate,
init_func = init,
frames = T,
interval = 20,
blit = True)
# saves the animation in our desktop
writergif = PillowWriter(fps=24)
anim.save('pedestrians.gif',writer=writergif)
uj5u.com熱心網友回復:
而不是plt.circle(在我看來 Matplotlib v3.5.1 不支持),請使用
另一種方法是存盤在每一幀繪制的圓圈,并在繪制新幀之前使它們不可見,同時使用fig.canvas.draw():
circles = {}
ax.set_aspect("equal")
def animate(frame):
global circles
xdata = np.random.rand(3)*10
ydata = np.random.rand(3)*10
for i in circles.keys():
circles[i].set_visible(False)
for i in range(3):
circles[i] = Circle((xdata[i], ydata[i]), 2, color='r', fill=False)
ax.add_patch(circles[i])
points, = ax.plot(xdata, ydata, 'o', color='b')
fig.canvas.draw()
return points,
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/535491.html
