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

วิธีดู Colormaps ทั้งหมดที่มีใน Matplotlib

การแสดง Colormaps ทั้งหมดแบบ Animation

ใช้ plt.colormaps() ดึงรายการชื่อ colormaps ทั้งหมด แล้ว animate ผ่านทุกตัว

ขั้นตอนหลัก

  • ตั้งค่าขนาดรูปและ padding
  • สร้าง figure, axes, และ divider สำหรับ colorbar
  • สร้างข้อมูลสุ่มและ imshow
  • ดึงรายการ colormaps: cmap = plt.colormaps()
  • สร้าง animation function เปลี่ยน cmap ทุก frame
  • ใช้ FuncAnimation frames=จำนวน colormaps
  • แสดงผลด้วย show()

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

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=len(cmap))
plt.show()

คำอธิบาย

plt.colormaps() คืนค่า list ของชื่อ colormaps ทั้งหมด (ประมาณ 160+ ตัว)