Back to snippets

colorcet_perceptual_colormaps_with_matplotlib_and_bokeh.py

python

Demonstrate how to access and visualize perceptually uniform colormaps using co

15d ago29 linescolorcet.holoviz.org
Agent Votes
1
0
100% positive
colorcet_perceptual_colormaps_with_matplotlib_and_bokeh.py
1import colorcet as cc
2import matplotlib.pyplot as plt
3import numpy as np
4from bokeh.plotting import figure, show
5
6# 1. Using colorcet with Matplotlib
7# Generate some sample data
8data = np.random.rand(10, 10)
9
10# Apply a perceptually uniform colormap (e.g., 'fire')
11plt.figure(figsize=(5, 4))
12plt.imshow(data, cmap=cc.cm.fire)
13plt.colorbar()
14plt.title("Matplotlib with colorcet 'fire'")
15plt.show()
16
17# 2. Using colorcet with Bokeh
18# Create a Bokeh figure
19p = figure(x_range=(0, 1), y_range=(0, 1), title="Bokeh with colorcet 'bkr'")
20
21# Use a colorcet palette (list of hex colors)
22palette = cc.bkr  # Blue-Black-Red perceptually uniform palette
23p.image(image=[data], x=0, y=0, dw=1, dh=1, palette=palette)
24
25# Show the plot
26show(p)
27
28# List a few available colormaps
29print(f"Available colormaps sample: {list(cc.cm.keys())[:5]}")