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

Cách xem tất cả colormap có sẵn trong Matplotlib?

Để xem tất cả colormap có sẵn trong Matplotlib:

  • Thiết lập kích thước hình và điều chỉnh khoảng đệm subplot.
  • Tạo figure mới.
  • Thêm axes vào figure.
  • Tạo axes divider.
  • Tạo dữ liệu ngẫu nhiên numpy.
  • Hiển thị dữ liệu như hình ảnh 2D.
  • Tạo colorbar cho ScalarMappable im.
  • Đặt tiêu đề figure.
  • Animate hình ảnh qua tất cả colormap.
  • Tạo animation bằng FuncAnimation.
  • Hiển thị hình bằng show().

Ví dụ

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from mpl_toolkits.axes_grid1 import make_axes_locatable
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
fig = plt.figure()
ax = fig.add_subplot(111)
div = make_axes_locatable(ax)
cax = div.append_axes('right', '5%', '5%')
data = np.random.rand(5, 5)
im = ax.imshow(data)
cb = fig.colorbar(im, cax=cax)
cmap = plt.colormaps()
tx = ax.set_title('Color map: {0}'.format(cmap[0]))
def animate(i):
    cax.cla()
    data = np.random.rand(5, 5)
    im = ax.imshow(data, cmap=cmap[i])
    fig.colorbar(im, cax=cax)
    tx.set_text('Color map: {0}'.format(cmap[i]))
ani = animation.FuncAnimation(fig, animate, frames=166)
plt.show()

Kết quả

Cách xem tất cả colormap có sẵn trong Matplotlib?