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

การเชื่อมสองจุดบน 3D Scatter Plot ใน Python และ Matplotlib


ขั้นตอนการทำงาน

  • กำหนดขนาดรูปภาพและปรับระยะห่างระหว่างและรอบๆ subplots
  • สร้าง figure ใหม่หรือเปิดใช้งาน figure ที่มีอยู่ด้วย figure()
  • เพิ่ม axes เข้าไปใน figure ปัจจุบันเป็น subplot arrangement
  • สร้างรายการ x, y และ z
  • พล็อต x, y, z ด้วย scatter()
  • เชื่อมจุดด้วย plot() พร้อม x, y, z สีดำ
  • ใช้เมธอด show() เพื่อแสดงรูปภาพ

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

from matplotlib import pyplot as plt

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

fig = plt.figure()
ax = fig.add_subplot(projection="3d")
x, y, z = [1, 1.5], [1, 2.4], [3.4, 1.4]
ax.scatter(x, y, z, c='red', s=100)
ax.plot(x, y, z, color='black')
plt.show()

ผลลัพธ์

การเชื่อมสองจุดบน 3D Scatter Plot ใน Python และ Matplotlib