แปลงแอนิเมชันใน Python

Feb 24 2023
ในบทความนี้ ฉันจะแสดงวิธีทำให้พล็อตเคลื่อนไหวใน Python โดยใช้ Matplotlib เพื่อให้คุณสามารถเพิ่มสัมผัสที่ดีให้กับการแสดงภาพของคุณได้! Matplotlib Matplotlib เป็นแพ็คเกจการวางแผนสำหรับภาษาโปรแกรม Python เป็นหนึ่งในแพ็คเกจยอดนิยมสำหรับการแสดงภาพใน Python และให้ผู้ใช้สามารถสร้าง 2-D, 3-D และแม้กระทั่งภาพเคลื่อนไหว

ในบทความนี้ ฉันจะแสดงวิธีทำให้พล็อตเคลื่อนไหวใน Python โดยใช้Matplotlibเพื่อให้คุณสามารถเพิ่มสัมผัสที่ดีให้กับการแสดงภาพของคุณได้!

โลโก้ Matplotlib

Matplotlib

Matplotlib เป็นแพ็คเกจการวางแผนสำหรับภาษาโปรแกรม Python เป็นหนึ่งในแพ็คเกจยอดนิยมสำหรับการแสดงภาพใน Python และให้ผู้ใช้สามารถสร้าง 2-D, 3-D และแม้กระทั่งภาพเคลื่อนไหว Matplotlib มีตัวเลือกที่ปรับแต่งได้มากมายสำหรับการสร้างพล็อต รวมถึงพล็อตเส้น พล็อตกระจาย แผนภูมิแท่ง และฮิสโตแกรม เนื่องจากหลายๆ คนคุ้นเคยกับการใช้ Matplotlib สำหรับการแสดงภาพแบบคงที่แล้ว ฉันจะอธิบายถึงการใช้ Matplotlib สำหรับโครงเรื่องแบบเคลื่อนไหว สำหรับผู้ที่ยังใหม่กับ Matplotlib แต่คุ้นเคยกับ Python อยู่แล้วนี่เป็นแหล่งข้อมูลที่ดีในการเริ่มต้นใช้งานแพ็คเกจ

พล็อตแอนิเมชั่น 2 มิติขั้นพื้นฐาน

#Empty lists for X/Y data
x = []
y = []

#Create a figure and axis to plot on
fig, ax = plt.subplots()


#We will create a function that will be called by Matplotlib's FuncAnimation
def getAnimation(iterator):
    newInt = random.randint(1,10)
    #Append values to our lists
    x.append(iterator)
    y.append(newInt)
    
    #Clear previous data from the plot
    ax.clear()
    #Plot the lists with axis limits
    ax.plot(x, y)
    ax.set_xlim([0,20])
    ax.set_ylim([0,20])
    
    
#We pass FuncAnimation our getAnimation function so that it is called in a loop
animation = FuncAnimation(fig, getAnimation, frames=20, 
            interval=500, repeat=False)

plt.show()

FuncAnimation()ที่ให้บริการผ่าน Matplotlib ต้องการตัวเลขและฟังก์ชันที่จะรันวนซ้ำด้วยจำนวนเฟรมจำนวนหนึ่ง โดยผ่านgetAnimation()เป็นอาร์กิวเมนต์ เรากำลังสร้างสไลด์โชว์ของพล็อตที่รวมกันเป็นแอนิเมชั่น โดยวนซ้ำตามจำนวนเฟรม นี่คือผลลัพธ์ของรหัสของเรา…

แอนิเมชั่นพื้นฐาน การพล็อตค่า y แบบสุ่มสำหรับค่า x แต่ละค่า

แอนิเมชั่นเดิน 3 มิติแบบสุ่ม

#Empty lists for X/Y/Z data
x = []
y = []
z = []

#Lets start off at (10,10,10) the center of our figure with axis limits 20
x.append(10)
y.append(10)
z.append(10)

#Total number of steps we will take
stepTotal = 20

#Create a figure and axis to plot on (3-D)
fig = plt.figure()
ax = fig.add_subplot(projection="3d")

#We will create a function that will be called by Matplotlib's FuncAnimation
def randomWalk(step):
    
    #Append values to our lists
    x.append(random.uniform(1,10))
    y.append(random.uniform(1,10))
    z.append(random.uniform(1,10))
    
    #Clear previous data from the plot
    ax.clear()
    #Plot the lists with axis limits
    ax.plot(x, y, z)
    ax.set_xlim([0,20])
    ax.set_ylim([0,20])
    ax.set_zlim([0,20])
    

# Creating the Animation object
animation = FuncAnimation(fig, randomWalk, frames=stepTotal, interval=100)

plt.show()

เดินสุ่มในพื้นที่ 3 มิติ

การบันทึกภาพเคลื่อนไหว


#FuncAnimation() returns an animation object that can be stored
animation = FuncAnimation()

#Instantiate the directory you wish to save to
directory = "/medium/animation.gif"

#We call .save() on our animation object and pass the function a directory
animation.save(directory)

รหัสทั้งหมดในบทความนี้สามารถพบได้ในGithub ของ ฉัน

หากคุณพบว่าบทความขนาดกลางนี้น่าสนใจ โปรดติดตามฉัน! ฉันเขียนเกี่ยวกับคณิตศาสตร์ วิทยาการคอมพิวเตอร์ และทุกๆ อย่างในระหว่างนั้น!