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

Matplotlib พลอตสมการ Implicit ได้หรือไม่?


ภาพรวม

Matplotlib ไม่มีฟังก์ชันโดยตรงสำหรับพลอตสมการ Implicit (เช่น sin(x) - cos(y)² = 0) แต่สามารถทำได้โดยใช้ Contour Plot

ขั้นตอน (Contour Method)

  1. สร้าง Grid X, Y ด้วย np.meshgrid
  2. คำนวณค่าสมการ Z = f(X, Y)
  3. ใช้ plt.contour(X, Y, Z, levels=[0]) เพื่อพลอตเส้น Contour ระดับ 0
  4. แสดงผล

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

from matplotlib import pyplot as plt
import numpy as np

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

delta = 0.025
xrange = np.arange(-5.0, 20.0, delta)
yrange = np.arange(-5.0, 20.0, delta)
x, y = np.meshgrid(xrange, yrange)

equation = np.sin(x) - np.cos(y)**2
plt.contour(x, y, equation, [0])
plt.show()

ผลลัพธ์

เส้นโค้งของสมการ sin(x) - cos(y)² = 0 แสดงเป็น Contour Line

เครื่องมือเสริม

ใช้ scipy.optimize หรือไลบรารี sympy.plotting.plot_implicit สำหรับการพลอต Implicit Equations ที่ซับซ้อนกว่า