1. 基础色带预览¶
演示如何通过 multicolor 获取、预览、保存色带。
获取色带¶
通过名称或 ID 获取 matplotlib.colors.Colormap 对象。
In [1]:
Copied!
from multicolor import get, get_by_id
# 按名称获取
viridis = get("Viridis")
print(f"Name: {viridis.name}")
print(f"Type: {type(viridis).__name__}")
# 按 ID 获取
cividis = get_by_id(10)
print(f"ID 10: {cividis.name}")
from multicolor import get, get_by_id
# 按名称获取
viridis = get("Viridis")
print(f"Name: {viridis.name}")
print(f"Type: {type(viridis).__name__}")
# 按 ID 获取
cividis = get_by_id(10)
print(f"ID 10: {cividis.name}")
Name: Viridis Type: LinearSegmentedColormap ID 10: Greys
预览色带¶
preview() 快速显示色带条。支持自定义方向、尺寸、标题。
In [2]:
Copied!
from multicolor import cmap
# 默认水平色带
cmap.preview(cmap.get("Viridis"), title="Viridis")
from multicolor import cmap
# 默认水平色带
cmap.preview(cmap.get("Viridis"), title="Viridis")
In [3]:
Copied!
# 垂直方向 + 自定义尺寸
cmap.preview(
cmap.get("Plasma"), orientation="vertical", size=(1, 6), title="Plasma (vertical)"
)
# 垂直方向 + 自定义尺寸
cmap.preview(
cmap.get("Plasma"), orientation="vertical", size=(1, 6), title="Plasma (vertical)"
)
列出所有色带¶
list() 返回 (id, name, cmap) 元组列表。names() 只返回名称(轻量级)。
In [4]:
Copied!
# 仅内置色带(排除用户自定义)
print("=== Built-in colormaps ===")
for cmap_id, name, cm in cmap.list(is_custom=False):
print(f" [{cmap_id}] {name}")
# 仅内置色带(排除用户自定义)
print("=== Built-in colormaps ===")
for cmap_id, name, cm in cmap.list(is_custom=False):
print(f" [{cmap_id}] {name}")
=== Built-in colormaps === [1] Accent [38] Afmhot [39] Autumn [40] Berlin [41] Binary [2] Blues [42] Bone [3] BrBG [4] BuGn [5] BuPu [43] Bwr [6] CMRmap [44] Cividis [45] Cool [46] Coolwarm [47] Copper [48] Cubehelix [7] Dark2 [49] Flag [50] Gist_earth [51] Gist_gray [52] Gist_heat [53] Gist_ncar [54] Gist_rainbow [55] Gist_stern [56] Gist_yarg [8] GnBu [57] Gnuplot [58] Gnuplot2 [59] Gray [9] Greens [10] Greys [60] Hot [61] Hsv [62] Inferno [63] Jet [64] Magma [65] Managua [66] Nipy_spectral [67] Ocean [11] OrRd [12] Oranges [13] PRGn [14] Paired [15] Pastel1 [16] Pastel2 [17] PiYG [68] Pink [69] Plasma [70] Prism [18] PuBu [19] PuBuGn [20] PuOr [21] PuRd [22] Purples [71] Rainbow [23] RdBu [24] RdGy [25] RdPu [26] RdYlBu [27] RdYlGn [28] Reds [72] Seismic [29] Set1 [30] Set2 [31] Set3 [32] Spectral [73] Spring [74] Summer [75] Tab10 [76] Tab20 [77] Tab20b [78] Tab20c [79] Terrain [80] Turbo [81] Twilight [82] Twilight_shifted [83] Vanimo [84] Viridis [85] Winter [33] Wistia [34] YlGn [35] YlGnBu [36] YlOrBr [37] YlOrRd
In [5]:
Copied!
# 轻量模式:只需名称列表
print(cmap.names(is_custom=False))
# 轻量模式:只需名称列表
print(cmap.names(is_custom=False))
['Accent', 'Afmhot', 'Autumn', 'Berlin', 'Binary', 'Blues', 'Bone', 'BrBG', 'BuGn', 'BuPu', 'Bwr', 'CMRmap', 'Cividis', 'Cool', 'Coolwarm', 'Copper', 'Cubehelix', 'Dark2', 'Flag', 'Gist_earth', 'Gist_gray', 'Gist_heat', 'Gist_ncar', 'Gist_rainbow', 'Gist_stern', 'Gist_yarg', 'GnBu', 'Gnuplot', 'Gnuplot2', 'Gray', 'Greens', 'Greys', 'Hot', 'Hsv', 'Inferno', 'Jet', 'Magma', 'Managua', 'Nipy_spectral', 'Ocean', 'OrRd', 'Oranges', 'PRGn', 'Paired', 'Pastel1', 'Pastel2', 'PiYG', 'Pink', 'Plasma', 'Prism', 'PuBu', 'PuBuGn', 'PuOr', 'PuRd', 'Purples', 'Rainbow', 'RdBu', 'RdGy', 'RdPu', 'RdYlBu', 'RdYlGn', 'Reds', 'Seismic', 'Set1', 'Set2', 'Set3', 'Spectral', 'Spring', 'Summer', 'Tab10', 'Tab20', 'Tab20b', 'Tab20c', 'Terrain', 'Turbo', 'Twilight', 'Twilight_shifted', 'Vanimo', 'Viridis', 'Winter', 'Wistia', 'YlGn', 'YlGnBu', 'YlOrBr', 'YlOrRd']
轻量元数据查询¶
records() 返回原始记录字典(不构建 Colormap 对象),适合只需要元数据的场景。
In [6]:
Copied!
recs = cmap.records(is_custom=False, limit=2)
for r in recs:
print(
f"{r['name']}: {len(r['colors'])} colors, type={r['cmap_type']}, source={r['source']}"
)
recs = cmap.records(is_custom=False, limit=2)
for r in recs:
print(
f"{r['name']}: {len(r['colors'])} colors, type={r['cmap_type']}, source={r['source']}"
)
Accent: 8 colors, type=qualitative, source=['matplotlib'] Afmhot: 256 colors, type=sequential, source=['matplotlib']
渲染为图像数组 / 文件¶
ColormapRenderer 提供更底层的渲染控制——不需要 matplotlib 窗口,直接拿到 numpy 数组或保存 PNG。
In [7]:
Copied!
import matplotlib.pyplot as plt
from multicolor.render import ColormapRenderer
cmap_obj = cmap.get("Viridis")
# 渲染为 RGBA numpy 数组(无窗口)
img = ColormapRenderer.to_image(cmap_obj, size=(5, 1), dpi=100)
print(f"RGBA array shape: {img.shape}")
# 渲染为 PNG 文件
ColormapRenderer.to_file(cmap_obj, "viridis_preview.png", size=(8, 1), dpi=200)
print("Saved viridis_preview.png")
# 显示刚才保存的文件
plt.figure(figsize=(8, 1))
plt.imshow(img, aspect="auto")
plt.axis("off")
plt.tight_layout()
plt.show()
import matplotlib.pyplot as plt
from multicolor.render import ColormapRenderer
cmap_obj = cmap.get("Viridis")
# 渲染为 RGBA numpy 数组(无窗口)
img = ColormapRenderer.to_image(cmap_obj, size=(5, 1), dpi=100)
print(f"RGBA array shape: {img.shape}")
# 渲染为 PNG 文件
ColormapRenderer.to_file(cmap_obj, "viridis_preview.png", size=(8, 1), dpi=200)
print("Saved viridis_preview.png")
# 显示刚才保存的文件
plt.figure(figsize=(8, 1))
plt.imshow(img, aspect="auto")
plt.axis("off")
plt.tight_layout()
plt.show()
RGBA array shape: (100, 500, 4) Saved viridis_preview.png