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

การพลอตจำนวน مختلط (Complex Numbers) แผนภาพ Argand ใน Matplotlib

จำนวน مختلط a + bj มีส่วนจริง (real) และจินตภาพ (imag) ใช้ data.real และ data.imag เป็นพิกัด x, y ใน ax.scatter() สร้าง Argand diagram ได้ง่าย

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

  1. ตั้งค่าขนาดรูปและเลย์เอาต์
  2. สร้างข้อมูล complex แบบสุ่ม: np.random.rand(10) + 1j*np.random.rand(10)
  3. สร้าง figure และ axes
  4. พลอต scatter: ax.scatter(data.real, data.imag, c=data.real, cmap='RdYlBu_r')
  5. แสดงผล

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

import numpy as np
from matplotlib import pyplot as plt

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

data = np.random.rand(10) + 1j*np.random.rand(10)
fig, ax = plt.subplots()
ax.scatter(data.real, data.imag, c=data.real, cmap="RdYlBu_r")
plt.show()

ผลลัพธ์

การพลอตจำนวน مختلط (Complex Numbers) แผนภาพ Argand ใน Matplotlib