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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With