Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีบันทึก Matplotlib 3D Rotating Plots

การบันทึก 3D Plot ที่หมุนได้

หมุนมุมมอง 3D ด้วย view_init() ในลูป แล้วใช้ plt.draw() และ plt.pause()

ขั้นตอนหลัก

  • ตั้งค่าขนาดรูปและ padding
  • สร้าง figure และ 3D axes ด้วย projection='3d'
  • ดึงข้อมูลทดสอบ 3D ด้วย axes3d.get_test_data()
  • พล็อต wireframe
  • ลูปหมุนมุมมอง 0-360 องศา ด้วย ax.view_init(30, angle)
  • ใช้ plt.draw() และ plt.pause(.001) อัปเดตการแสดงผล
  • แสดงผลด้วย show()

ตัวอย่างโค้ด

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X, Y, Z = axes3d.get_test_data(0.1)
ax.plot_wireframe(X, Y, Z, rstride=5, cstride=5)

for angle in range(0, 360):
    ax.view_init(30, angle)
    plt.draw()
    plt.pause(.001)

plt.show()

คำอธิบาย

view_init(elev, azim) กำหนดมุมมอง elevation และ azimuth angle

หมายเหตุ

เพื่อบันทึกเป็นไฟล์วิดีโอ ใช้ animation.FuncAnimation และ writer