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

ตั้งค่า Colormap ให้ภาพใน Matplotlib

ใช้ imshow() พร้อมพารามิเตอร์ cmap เช่น 'hot', 'viridis', 'plasma' เป็นต้น

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


from matplotlib import pyplot as plt, image as mimg

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

img = mimg.imread('bird.jpg')
lum_img = img[:, :, 0]  # เลือก channel แรก (Red)
plt.imshow(lum_img, cmap="hot")
plt.axis('off')
plt.show()

ผลลัพธ์

ภาพ grayscale แสดงด้วย colormap "hot" (ดำ-แดง-เหลือง-ขาว)

ตั้งค่า Colormap ให้ภาพใน Matplotlib