Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using colormap with bokeh scatter

In matplotlib the scatterplot offers the possibility of using the color of a plot to indicate value or magnitude like this plot:

enter image description here

For bokeh, similar examples seem to manually generate the rgb colors, which makes it difficult to produce plots with color scaled by magnitude, esp. wrt. diverging colormaps.

Is it possible to have similar functionality in bokeh, or to use matplotlib colormaps to set the color?

like image 247
M.T Avatar asked Feb 10 '16 12:02

M.T


2 Answers

Another option if you want to use a field name, is to use a LinearColorMapper:

from bokeh.models import LinearColorMapper

color_mapper = LinearColorMapper(palette='Magma256', low=min(radii), high=max(radii))

p.scatter(x,y,color={'field': 'radii', 'transform': color_mapper})
like image 145
shortcipher3 Avatar answered Oct 05 '22 06:10

shortcipher3


It's easy enough to just use matplotlib's colormaps directly. For example, the following uses viridis in bokeh's example (note that I'm using a jupyter notebook):

import numpy as np

from bokeh.plotting import figure, show, output_notebook
import matplotlib as mpl

output_notebook()

N = 4000
x = np.random.random(size=N) * 100
y = np.random.random(size=N) * 100
radii = np.random.random(size=N) * 1.5
colors = [
    "#%02x%02x%02x" % (int(r), int(g), int(b)) for r, g, b, _ in 255*mpl.cm.viridis(mpl.colors.Normalize()(radii))
]

p = figure()

p.scatter(x, y, radius=radii,
          fill_color=colors, fill_alpha=0.6,
          line_color=None)

show(p)  

Essentially, for any matplotlib colormap in cm, initializing it with an array of values will return an array with each value replaced by [r,g,b,a] values in the range [0,1]. Note that this assumes all the values are between 0 and 1 as well; here I use matplot.colors.Normalize to ensure this.

like image 44
cge Avatar answered Oct 05 '22 08:10

cge