具有渐变颜色的散景散点图

Bokeh scatterplot with gradient colors

我正在尝试使用 Bokeh 绘制散点图。例如:

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)

理想情况下,随着数据接近 y 的 maximum/minimum 值,我希望使用更强的颜色进行着色。例如从红色到蓝色(-1 到 1),就像在 heatmap 中一样(参数 vmaxvmin)。

有简单的方法吗?

Bokeh 具有将值映射到颜色的内置功能,然后将它们应用于绘图字形。

您也可以为每个点创建一个颜色列表,如果您不想使用此功能,则将其传入。

看下面一个简单的例子:

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)