Bokeh: Custom color palettes with the image glyph

Created on 4 Mar 2015  路  3Comments  路  Source: bokeh/bokeh

The built-in color palettes (brewer) support upto 11 color values. Was there a reason for limiting this to such a small number? I want to plot my continuous (non-categorical) scalar data with a smooth colormap, like this

N = 100
x = np.linspace(0, 10, N)
y = np.linspace(0, 10, N)
xx, yy = np.meshgrid(x, y)
d = np.sin(xx)*np.cos(yy)

figure()
ax=subplot(111)
ax.pcolormesh(xx,yy,d,cmap='Spectral')

image

The default pyplot colormaps have 256 values. I have tried sending plotting.image() a custom palette based on matplotlib's, but in passing it to bokeh, i seem to only get the blue channel??

N = 100
x = np.linspace(0, 10, N)
y = np.linspace(0, 10, N)
xx, yy = np.meshgrid(x, y)
d = np.sin(xx)*np.cos(yy)

custom=[]
for n in arange(0,256):
    rgba0_1 = cm.Spectral(n)#get 4-tuple RGBa
    rgb0_1 = array(rgba0_1[0:3])#drop the alpha value, convert to array
    rgb0_255 = tuple(int(n) for n in rgb0_1*255)#rescale RBG as ints, back to tuple
    custom.append(tuple(rgb0_255))

p = bk.figure(x_range=[0, 10], y_range=[0, 10])
p.image(image=[d], x=[0], y=[0], dw=[10], dh=[10], palette=custom)

bk.show(p)

image

Is this a bug, or me doing something wrong? I would like to see more native support of denser palettes in the long term. And short term, any suggestions how i can work around this?

wontfix notebook discussion

Most helpful comment

This will convert a matplotlib colormap into a bokeh palette:

import matplotlib as plt
import matplotlib.cm as cm
import numpy as np

colormap =cm.get_cmap("Blues") #choose any matplotlib colormap here
bokehpalette = [plt.colors.rgb2hex(m) for m in colormap(np.arange(colormap.N))]

All 3 comments

There is no inherent limitation AFAIK. To create your own palette, from bokeh.models.mappers import LinearColorMapper and then pass a list of the hex codes you want to use when you create the LinearColorMapper. Once that is created, instead of the palette keyword, use color_mapper. So something like .., color_mapper=LinearColorMapper(SSTPalette).

I am using a large palette and it works fine.

Discussion is now more than 3 months old. Can re-open if renewed interest.

This will convert a matplotlib colormap into a bokeh palette:

import matplotlib as plt
import matplotlib.cm as cm
import numpy as np

colormap =cm.get_cmap("Blues") #choose any matplotlib colormap here
bokehpalette = [plt.colors.rgb2hex(m) for m in colormap(np.arange(colormap.N))]
Was this page helpful?
0 / 5 - 0 ratings

Related issues

maeglin89273 picture maeglin89273  路  3Comments

darribas picture darribas  路  3Comments

nmvasani picture nmvasani  路  3Comments

jnettels picture jnettels  路  3Comments

tkchris93 picture tkchris93  路  3Comments