LogCapy
LogCapy

Reputation: 467

Using matplotlib's colormap for bokeh's color bar

I want to create a bokeh ColorBar using matplotlib's coolwarm colormap. Ticks are not necessary. How would I do this?

Upvotes: 1

Views: 1769

Answers (1)

Y. Luo
Y. Luo

Reputation: 5722

The key here is to get a valid palette for matplotlib's coolwarm colormap. bokeh.palette module has only four matplotlib palettes, not including coolwarm. You need to make it yourself (or use colorcet package as explained later).

A typical big bokeh.palette has 256 colors in a list of (hex) RGB color strings. A general idea is to sample 256 colors from matplotlib coolwarm colormap and covert them into a list of (hex) RGB color strings. Instantiate the coolwarm class with a list of numbers will return a list of RGBA colors. RGB values for each color are within [0, 1] and A=1.0. bokeh.colors.RGB requires input RGBA one at a time with RGBA being independent int arguments within [0, 255].

from bokeh.colors import RGB
from matplotlib import cm

m_coolwarm_rgb = (255 * cm.coolwarm(range(256))).astype('int')
coolwarm_palette = [RGB(*tuple(rgb)).to_hex() for rgb in m_coolwarm_rgb]

You can now use coolwarm_palette as a palette (replacing "Viridis256") in line:

color_mapper = LogColorMapper(palette="Viridis256", low=1, high=1e7)

Alternatively, you can use colorcet package if that is a viable option for you. colorcet.coolwarm is the palette to use here. However, as you might see after trying them yourself, the final plots are slightly different in color distribution due to differences in color sampling.

Upvotes: 10

Related Questions