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

วาดเส้นโค้งพารามิเตอร์ด้วย pyplot.plot() ใน Matplotlib


เพื่อวาดเส้นโค้งพารามิเตอร์ (parametric curve) ด้วย pyplot.plot()

  • ตั้งค่าขนาดรูปและ padding ระหว่าง subplots
  • กำหนดตัวแปร N สำหรับจำนวนตัวอย่าง
  • สร้าง t, r, x, y ด้วย numpy
  • สร้าง figure และ subplots
  • ใช้ plot() พลอต x และ y
  • แสดงรูปด้วย show()

ตัวอย่าง

import numpy as np
import matplotlib.pyplot as plt

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

N = 400
t = np.linspace(0, 2 * np.pi, N)
r = 0.5 + np.cos(t)
x, y = r * np.cos(t), r * np.sin(t)

fig, ax = plt.subplots()
ax.plot(x, y)
plt.show()

ผลลัพธ์

วาดเส้นโค้งพารามิเตอร์ด้วย pyplot.plot() ใน Matplotlib