color_mappers#
一个带有颜色频谱比例的颜色映射图。示例图演示了对数映射和线性映射,使用不同的颜色调色板。
详细信息
- Bokeh API:
figure.scatter
,bokeh.models.ColumnDataSource
,bokeh.models.ColorBar
,bokeh.models.LinearColorMapper
,bokeh.models.LogColorMapper
- 更多信息:
- 关键词:
颜色,工具,散点图,数据映射
# noqa: E501
import numpy as np
from bokeh.layouts import column, gridplot
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, show
from bokeh.transform import linear_cmap, log_cmap
x = np.random.random(size=2000) * 1000
y = np.random.normal(size=2000) * 2 + 5
source = ColumnDataSource(dict(x=x, y=y))
def make_plot(mapper, palette):
cmap = mapper("x", palette=palette, low=1, high=1000)
axis_type = mapper.__name__.split("_")[0] # linear or log
p = figure(x_range=(1, 1000), title=f"{palette} with {mapper.__name__}",
toolbar_location=None, tools="", x_axis_type=axis_type)
r = p.scatter('x', 'y', alpha=0.8, source=source, color=cmap)
color_bar = r.construct_color_bar(padding=0,
ticker=p.xaxis.ticker,
formatter=p.xaxis.formatter)
p.add_layout(color_bar, 'below')
return p
p1 = make_plot(linear_cmap, "Viridis256")
p2 = make_plot(log_cmap, "Viridis256")
p3 = make_plot(linear_cmap, "Viridis6")
p4 = make_plot(log_cmap, "Viridis6")
p5 = figure(x_range=(1, 1000), width=800, height=300, toolbar_location=None, tools="",
title="Viridis256 with linear_cmap, low/high = 200/800 = pink/grey")
cmap = linear_cmap("x", palette="Viridis256", low=200, high=800,
low_color="pink", high_color="darkgrey")
p5.scatter(x='x', y='y', alpha=0.8, source=source, color=cmap)
grid = gridplot([[p1, p2], [p3, p4]], width=400, height=300, toolbar_location=None)
show(column(grid, p5))