Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Tạo colormap Parula trong Matplotlib

Để tạo colormap Parula (mặc định của MATLAB) trong Matplotlib, dùng LinearSegmentedColormap.from_list() từ danh sách màu. Có thể dùng công cụ viscm để phân tích.

Các bước

  • Thiết lập figure size và autolayout.
  • Import LinearSegmentedColormap từ matplotlib.colors.
  • Tạo dữ liệu màu ngẫu nhiên (thay bằng dữ liệu Parula thực tế).
  • Tạo colormap: parula_map = LinearSegmentedColormap.from_list('parula', cm_data).
  • Phân tích colormap bằng viscm(parula_map) (nếu cài viscm).
  • Hiển thị colormap bằng imshow().
  • Hiển thị bằng show().

Ví dụ

from matplotlib.colors import LinearSegmentedColormap
import matplotlib.pyplot as plt
import numpy as np
from viscm import viscm

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

cm_data = np.random.rand(4, 4)
parula_map = LinearSegmentedColormap.from_list('parula', cm_data)
viscm(parula_map)
plt.imshow(np.linspace(0, 100, 256)[None, :], aspect='auto', cmap=parula_map)
plt.show()

Kết quả

Tạo colormap Parula trong Matplotlib