1

I am using the Python library bokeh and was wondering whether it is possible to have a continuous colour scale (or colour bar) with scatter plots.

Currently, it is straightforward to have a legend with colour groups but not a continuous colour scale like in heatmaps.

Any assistance please?

Martin Evans
  • 45,791
  • 17
  • 81
  • 97
french
  • 13
  • 1
  • 5

1 Answers1

2

Here is a discussion of the color palettes in bokeh: Custom color palettes with the image glyph

Notice the code snippet at the bottom on how to create a bokeh palette from a matplotlib colormap.

However, I find it more convenient to create a separate color channel from the matplotlib colormap directly:

import numpy as np
import matplotlib.cm as cm

import bokeh.plotting as bk

# generate data
N = 4000
x = np.random.random(size=N) * 100
y = np.random.random(size=N) * 100
radii = np.random.random(size=N) * 1.5

# get a colormap from matplotlib
colormap =cm.get_cmap("gist_rainbow") #choose any matplotlib colormap here

# define maximum and minimum for cmap
colorspan=[40,140]

# create a color channel with a value between 0 and 1
# outside the colorspan the value becomes 0 (left) and 1 (right)
cmap_input=np.interp(np.sqrt(x*x+y*y),colorspan,[0,1],left=0,right=1)

# use colormap to generate rgb-values
# second value is alfa (not used)
# third parameter gives int if True, otherwise float
A_color=colormap(cmap_input,1,True)

# convert to hex to fit to bokeh
bokeh_colors = ["#%02x%02x%02x" % (r, g, b) for r, g, b in A_color[:,0:3]]

# create the plot-
p = bk.figure(title="Example of importing colormap from matplotlib")

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

bk.output_file("rainbow.html")

bk.show(p)  # open a browser

I hope this helps!

Anadyn
  • 81
  • 3
  • Hi @Anadyn, Many thanks for the useful code, can we add a continuous colorbar? – french Jul 08 '16 at 08:55
  • Hi @french, I think you have to do it by hand, see this [discussion](http://stackoverflow.com/questions/32614953/can-i-plot-a-colorbar-for-a-bokeh-heatmap) – Anadyn Jul 09 '16 at 10:47