ภาพรวม
สาธิตการสร้าง Custom Colormap จากรายการสี และใช้กับ imshow()
ขั้นตอน
- ตั้งค่า Figure
- สร้างข้อมูลสุ่ม 5x5
- สร้าง Colormap ด้วย
ListedColormap(['r', 'g', 'b']) - พลอตด้วย
plt.imshow(data, cmap=cmap) - แสดงผล
ตัวอย่างโค้ด
from matplotlib import pyplot as plt
from matplotlib.colors import ListedColormap
import numpy as np
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
data = np.random.rand(5, 5)
cmap = ListedColormap(['r', 'g', 'b'])
plt.imshow(data, cmap=cmap)
plt.show()
ผลลัพธ์
Heatmap 5x5 ใช้สีแดง เขียว น้ำเงิน แทนค่าต่ำ กลาง สูง
การสร้าง Colormap แบบอื่น
LinearSegmentedColormap.from_list('name', ['red', 'yellow', 'green'])- Gradientplt.cm.get_cmap('viridis')- Built-in Colormapscmap.set_under('gray'),cmap.set_over('white')- สีสำหรับค่านอกขอบเขต