皆さん、こんにちは!
この記事はPythonのデータ可視化ライブラリである「Bokehを学ぶシリーズ」の第5回目。
公式ドキュメントのFirst Stepを更に詳しく解説するようなイメージで進めていきます。


この記事では、第5回目ということで、公式ドキュメントのFirst Stepの1つ「Customizing your plot」の後半を解説していきます。
シリーズの別記事を以下にまとめておきます!
色のベクトル化
色のベクトル化では、グラフで描画するオブジェクト(〇や▼など)の色付けを、変数の値を用いて変更させるようなものを指します。
これまで紹介してきたグラフは、どれもオブジェクトの色は同じでしたが、こちらを用いることでより視覚的にグラフを描画することができます。
従来のグラフで色を割り当てるには、fill_colorを用いてきましたが、今回のベクトル化でもfill_colorに対して数値を与えます。
import random
from bokeh.plotting import figure, show
# XとYのデータセットを作成
x = list(range(0, 26))
y = random.sample(range(0, 100), 26)
# rbbカラーをリスト形式で作成 yのデータを変数に、G(Green)の色を低下させる
colors = ["#%02x%02x%02x" % (255, int(round(value * 255 / 100)), 255) for value in y]
# 新しいプロットの作成
p = figure(
title="Vectorized colors example",
sizing_mode="stretch_width",
max_width=500,
height=250,
)
# 折れ線グラフとバブルチャートを作成
line = p.line(x, y, line_color="blue", line_width=1)
circle = p.circle(x, y, fill_color=colors, line_color="blue", size=15)
# 結果を描画
show(p)

例えば、RGBの各パラメータを調整することで、以下のような色合いにすることも可能です。
値が小さいほど、赤色が濃くなるという微妙な感じですが、色々と使いようはありそうです。
colors = ["#%02x%02x%02x" % (255, int(round(2.5 *value)), 0) for value in y]

色とサイズのベクトル化
次はグラフ内の色とプロットのサイズを変更していきます。
以下のようにグラフの要素サイズは、radii = y / 100 * 2のように数式を代入することが可能です。
色サイズと共に、グラフの要素をYの値によって可変にすることが可能です。
import numpy as np
from bokeh.plotting import figure, show
# ランダム関数でデータを作成
N = 1000
x = np.random.random(size=N) * 100
y = np.random.random(size=N) * 100
# radii(グラフの要素サイズ)と色を可変にする
radii = y / 100 * 2
colors = ["#%02x%02x%02x" % (255, int(round(value * 255 / 100)), 255) for value in y]
# 新しいプロットの作成
p = figure(
title="Vectorized colors and radii example",
sizing_mode="stretch_width",
max_width=500,
height=250,
)
# バブルチャートを作成
p.circle(
x,
y,
radius=radii,
fill_color=colors,
fill_alpha=0.6,
line_color="lightgrey",
)
# 結果を描画
show(p)

コンターのようなカラーマッピングを行う
さて、ここまでは色を毎回のように定義してきましたが、あらかじめ用意されたカラーパレットを利用することも可能です。
Brewer、D3、Matplotlibなどが利用可能です。名前だけだとわかりにくいと思うので、Brewerの例を示しておきます。

こちらを、linear_cmap()関数を用いてデータの色付けを行います。
各変数の意味は以下です。
- field_name:色をマップする対象
- palette:カラーパレット名
- low:色を付ける最小値
- high:色をマップする最大値
from bokeh.io import show
from bokeh.palettes import Turbo256
from bokeh.plotting import figure
from bokeh.transform import linear_cmap
# データを生成
x = list(range(-32, 33))
y = [i**2 for i in x]
# 線の色を決定する
mapper = linear_cmap(field_name="y", palette=Turbo256, low=min(y), high=max(y))
# 新しいプロットの作成
p = figure(width=500, height=250)
# バブルチャートを作成
p.circle(x, y, color=mapper, size=10)
# 結果を描画
show(p)

後は、IF文を複数用意することで、各数値で閾値を決めて、色を切り替えてやることも可能です。
こちらの方がコンターぽさが出るかもしれません。
from bokeh.models import ColorBar, ColumnDataSource
from bokeh.palettes import Spectral6
from bokeh.plotting import figure, output_file, show
from bokeh.transform import linear_cmap
output_file("styling_linear_mappers.html", title="styling_linear_mappers.py example")
x = [1,2,3,4,5,7,8,9,10,11,12]
y = [1,2,3,4,5,7,8,9,10,5,2]
#Use the field name of the column source
mapper = linear_cmap(field_name='y', palette=Spectral6 ,low=min(y) ,high=max(y))
source = ColumnDataSource(dict(x=x,y=y))
p = figure(width=300, height=300, title="Linear Color Map Based on Y")
p.circle(x='x', y='y', line_color=mapper,color=mapper, fill_alpha=1, size=12, source=source)
color_bar = ColorBar(color_mapper=mapper['transform'], width=8)
p.add_layout(color_bar, 'right')
show(p)

まとめ
「Bokehを学ぶシリーズ」第6回はいかがでしたでしょうか。
Bokehのグラフ内で色を可変にする方法について扱いました。
経った数行でイイ感じのグラフを書くことが出来るBokehをマスターできれば、簡単に社内共有用のデータダッシュボードを作成することができそうです。
