Để tạo colormap Matplotlib xử lý đặc biệt một giá trị, chúng ta có thể thực hiện các bước sau:
- Thiết lập kích thước figure và điều chỉnh padding giữa và xung quanh subplots.
- Lấy instance colormap, tên là "rainbow".
- Đặt màu cho giá trị out-of-range thấp, dùng phương thức set_under('red').
- Tạo dữ liệu ngẫu nhiên và eps bằng numpy.
- Tạo figure và tập subplots.
- Hiển thị dữ liệu như hình ảnh, dùng phương thức imshow().
- Tạo colorbar cho instance ScalarMappable, im.
- Để hiển thị figure, dùng phương thức show().
Ví dụ
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
cmap = plt.get_cmap('rainbow')
cmap.set_under('red')
data = np.random.randn(5, 5)
eps = np.spacing(0.0)
fig, ax = plt.subplots()
im = ax.imshow(data, interpolation='nearest', vmin=eps, cmap=cmap)
fig.colorbar(im, extend='min')
plt.show()
Kết quả

