我有三個圓圈的位置資料,我想制作這些移動圓圈的影片。我看過很多功能影片,但我無法處理幾何形狀的影片。這是我撰寫的一些代碼,用于在時變位置創建三個圓圈的圖。
import matplotlib.pyplot as plt
import numpy as np
# Just a minimal example mock data
positions=np.zeros((3,2,100))
positions[0,0,:]=np.linspace(-10,10,100)
positions[0,1,:]=np.linspace(-10,10,100)
positions[1,0,:]=7*np.cos(np.linspace(0,30,100))
positions[1,1,:]=7*np.sin(np.linspace(0,30,100))
positions[2,0,:]=5*np.sin(np.linspace(0,30,100))
positions[2,1,:]=np.linspace(-10,10,100)
# My code
for i in range(100):
fig, ax = plt.subplots()
ax.set_xlim(-10, 10)
ax.set_ylim(-10, 10)
circle1=plt.Circle((positions[0,0,i], positions[0,1,i]),1)
circle2=plt.Circle((positions[1,0,i], positions[1,1,i]),1)
circle3=plt.Circle((positions[2,0,i], positions[2,1,i]),1)
ax.add_patch(circle1)
ax.add_patch(circle2)
ax.add_patch(circle3)
ax.axis("equal")
position 是一個陣列:第一個索引標記不同的圓,第二個索引是 x 和 y 坐標,第三個索引是第 i 個時間步。隨著位置的變化,我可以創建這些形狀的圖。但現在我想把這些影像放在一起制作影片。
uj5u.com熱心網友回復:
您需要呼叫 animate 函式。
(還有其他的方法。根據你渲染情節的方式,你可以自己回圈,并更新資料。但是對于這種影片,影片更好)。
# Let start with import. Btw, you should include those in your question.
# The "minimal reproducible example" is an example that is minimal, in the sense
# that it contains only what is strictly needed for your question,
# with nothing from your app that is not needed.
# But it contains also ALL that is needed to reproduce problem.
# You wouldn't want someone to reply to you "if your code doesn't work
# it is because you did not import matplotlib". But if you don't include
# it...
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as anim
# Likewise, a minimal reproducible example should include mock data
# Again, no need for the real one. I am sure they are complicated to
# create, and this complication if off topic to your question
# But anything, just to illustrate the question.
positions=np.zeros((3,2,100))
positions[0,0,:]=np.linspace(-10,10,100)
positions[0,1,:]=np.linspace(-10,10,100)
positions[1,0,:]=7*np.cos(np.linspace(0,30,100))
positions[1,1,:]=7*np.sin(np.linspace(0,30,100))
positions[2,0,:]=5*np.sin(np.linspace(0,30,100))
positions[2,1,:]=np.linspace(-10,10,100)
# That preamble done, we start with something very similar to one loop
# of your code, just to create the plot
i=0 # That is because I am lazy, and it's easier than replacing i by 0 in the following lines
fig, ax = plt.subplots()
ax.set_xlim(-10, 10)
ax.set_ylim(-10, 10)
circle1=plt.Circle((positions[0,0,i], positions[0,1,i]),1)
circle2=plt.Circle((positions[1,0,i], positions[1,1,i]),1)
circle3=plt.Circle((positions[2,0,i], positions[2,1,i]),1)
ax.add_patch(circle1)
ax.add_patch(circle2)
ax.add_patch(circle3)
#ax.axis("equal") # I comment this one because it breaks xlim/ylim
# So now, we have 1 plot, with the circles
# We DONT just redo it to with other animations. That wouldn't work (it creates
# a new figure) But even if it were working, that would be hugely slow
# Creating a figure is way more expansive than just updating some
# parameters of it
# What we do, is to regularly update the plot data, here, the centers
# of the 3 circles
# In this case (again, it could be done other way), I do so using a
# animation function that will be called back iteratively by the
# rendering loop
# animate has 1 arg. That is `i`, the frame number (the exact same i of your code)
# And returns a list of `artist` that is of "things" whose you want to
# update the data.
# and before returning it does the job of updating the data
def animate(i):
# We update the center of each of the 3 circles
circle1.set_center(positions[0,:,i])
circle2.set_center(positions[1,:,i])
circle3.set_center(positions[2,:,i])
# And return a list of those 3 circles so that the main loop knows
# that they were changed
return [circle1, circle2, circle3]
# This is how we ensure `animate` is called 100 times. With 20ms between each call.
# In fact, because of the last parameter, it is called infinitely, because
# we loop back to the beginning
theAnimation = anim.FuncAnimation(fig, animate, frames=100, interval=20, blit=True )
#theAnimation.save("result.gif")
plt.show()
所以,粗略地說,除了繁重的評論之外,這是您的代碼 3 行用于set_center在每個回圈中執行的操作,您自己的回圈只執行一次。這里的回圈是通過FuncAnimation迭代呼叫animate回呼來完成的。
而且,我將編輯您的問題以包含“最小可重現示例”(但如果您認為這與您的意圖相矛盾,您可以自由編輯它)。我鼓勵您嘗試使用相同型別的示例來解決您未來的問題。

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/531211.html
上一篇:如何將資料從RecyclerView的點擊監聽器傳遞到另一個片段?
下一篇:如何使用JS/jQuery.animate({影片元素從Last到First/ReverseAnimation的函式
