Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bokeh scatterplot with gradient colors

Tags:

python

plot

bokeh

I'm trying to make a scatter plot with Bokeh. For example:

from bokeh.plotting import figure, show, output_notebook

TOOLS='pan,wheel_zoom,box_zoom,reset'
p = figure(tools=TOOLS)

p.scatter(x=somedata.x, y=somedata.y)

Ideally, I'd like to color with stronger intensity as the data approaches its maximum/minimum value of y. For example from red to blue (-1 to 1), just like in a heatmap (parameters vmax and vmin).

Is there an easy way of doing it?

like image 664
zzzmk Avatar asked Dec 05 '17 10:12

zzzmk


1 Answers

Bokeh has an inbuilt functionality for mapping values to colors, and then applying these to the plot glyphs.

You can alternatively create a list of colors for each point, and pass these in if you don't want to use this features.

See below a simple example:

import numpy as np
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource, LinearColorMapper


TOOLS='pan,wheel_zoom,box_zoom,reset'
p = figure(tools=TOOLS)

x = np.linspace(-10,10,200)
y = -x**2

data_source = ColumnDataSource({'x':x,'y':y})

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

# specify that we want to map the colors to the y values, 
# this could be replaced with a list of colors
p.scatter(x,y,color={'field': 'y', 'transform': color_mapper})

show(p)
like image 150
Anthonydouc Avatar answered Oct 21 '22 12:10

Anthonydouc