การแสดงข้อมูล Boolean (True/False) เป็นภาพ
ใช้ imshow() แสดง boolean array เป็นภาพขาวดำ/สี
ขั้นตอนหลัก
- ตั้งค่าขนาดรูปและ padding
- สร้าง boolean array ด้วย np.random.random((20,20)) > 0.5
- สร้าง figure และ axes
- ใช้ imshow() พร้อม aspect='auto', cmap, interpolation
- แสดงผลด้วย show()
ตัวอย่างโค้ด
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
data = np.random.random((20, 20)) > 0.5
fig = plt.figure()
ax = fig.add_subplot(111)
ax.imshow(data, aspect='auto', cmap="copper", interpolation='nearest')
plt.show()
คำอธิบาย
- boolean array: True=1, False=0
- cmap="copper": แสดงสีทองแดง (True=สว่าง, False=มืด)
- aspect='auto': ปรับ aspect ratio ให้เต็ม axes
- interpolation='nearest': ไม่補間 พิกเซลชัดเจน