Reputation: 13
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?
Upvotes: 1
Views: 3801
Reputation: 81
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!
Upvotes: 2