3

環境

やりたいこと

データ可視化ライブラリBokehを用いて、以下のような「積み上げ面グラフ」を作成したいです。

画像の説明をここに入力

https://jp.cybozu.help/ja/k/user/graph_type 引用

Bokeh 0.12のときは、bokeh.charts.Areaを使って、簡単に書くことができます。

from bokeh.charts import Area, show, output_file, defaults
from bokeh.layouts import row

defaults.width = 400
defaults.height = 400

# create some example data
data = dict(
    python=[2, 3, 7, 5, 26, 221, 44, 233, 254, 265, 266, 267, 120, 111],
    pypy=[12, 33, 47, 15, 126, 121, 144, 233, 254, 225, 226, 267, 110, 130],
    jython=[22, 43, 10, 25, 26, 101, 114, 203, 194, 215, 201, 227, 139, 160],
)

area1 = Area(data, title="Area Chart", legend="top_left",
             xlabel='time', ylabel='memory')

area2 = Area(data, title="Stacked Area Chart", legend="top_left",
             stack=True, xlabel='time', ylabel='memory')

output_file("area.html", title="area.py example")

show(row(area1, area2))

https://bokeh.pydata.org/en/0.12.5/docs/gallery/area_chart.html 引用

しかし、Bokeh0.13.0では、bokeh.charts.Areaクラスがなくなっていました。

質問

Bokeh 0.13で、「積み上げ面グラフ」用を作成するのには、どのクラスを使えばよろしいでしょうか?
patchesメソッドを使って、「積み上げ面グラフ」を作成しているサンプルはありましたが、自分でstack値を計算していて、Areaより不便です。
もう少し便利なクラスやメソッドがありましたら、教えてください。

import numpy as np
import pandas as pd

from bokeh.plotting import figure, show, output_file
from bokeh.palettes import brewer

N = 20
cats = 10
df = pd.DataFrame(np.random.randint(10, 100, size=(N, cats))).add_prefix('y')

def  stacked(df):
    df_top = df.cumsum(axis=1)
    df_bottom = df_top.shift(axis=1).fillna({'y0': 0})[::-1]
    df_stack = pd.concat([df_bottom, df_top], ignore_index=True)
    return df_stack

areas = stacked(df)
colors = brewer['Spectral'][areas.shape[1]]
x2 = np.hstack((df.index[::-1], df.index))

p = figure(x_range=(0, N-1), y_range=(0, 800))
p.grid.minor_grid_line_color = '#eeeeee'

p.patches([x2] * areas.shape[1], [areas[c].values for c in areas],
          color=colors, alpha=0.8, line_color=None)

output_file('brewer.html', title='brewer.py example')

show(p)

https://bokeh.pydata.org/en/latest/docs/gallery/brewer.html 引用

2
  • あるバージョンで特定の機能がなくなった場合などは、リリースノートを確認すると、移行方法などが記載されていることが多いです。今後のトラブルシューティングの参考になると思います。Bokeh リリースノート 0.12.9 (Sep 2017)
    – user28902
    2018年7月19日 12:14
  • ありがとうございました。「Deprecated bokeh.charts support removed」という項目を見つけました。 一応リリースノートは簡単に見たんですが、「Area」で探していて「リリースノートに載っていないな」と判断してしまいました。 「bokeh.charts」自体が削除されたんですね。
    – yuji38kwmt
    2018年7月19日 15:42

2 件の回答 2

2

Bokehbkchartsの後継はHoloViewsです。HoloViewsは、Bokehをバックエンドにして簡潔な記述でグラフを描くことができます。

HoloViewsの面グラフのドキュメントとサンプルへのリンクは、次のとおりです。

ドキュメント: http://holoviews.org/reference/elements/bokeh/Area.html
サンプル: http://holoviews.org/gallery/demos/bokeh/area_chart.html

サンプルのコードは、次のとおりで、質問にあるBokeh 0.12のときの、bokeh.charts.Areaを使って書くのと同じグラフになっています。

import numpy as np
import holoviews as hv
hv.extension('bokeh')

# create some example data
python=np.array([2, 3, 7, 5, 26, 221, 44, 233, 254, 265, 266, 267, 120, 111])
pypy=np.array([12, 33, 47, 15, 126, 121, 144, 233, 254, 225, 226, 267, 110, 130])
jython=np.array([22, 43, 10, 25, 26, 101, 114, 203, 194, 215, 201, 227, 139, 160])

dims = dict(kdims='time', vdims='memory')
python = hv.Area(python, label='python', **dims)
pypy   = hv.Area(pypy,   label='pypy',   **dims)
jython = hv.Area(jython, label='jython', **dims)

overlay = (python * pypy * jython).options('Area', fill_alpha=0.5)
overlay.relabel("Area Chart") + hv.Area.stack(overlay).relabel("Stacked Area Chart")

なお、Plotするところで、+*の演算子がありますが、+は、グラフを並べる演算子で、*はグラフを重ねる演算子です。HoloViewsでは、+*の演算子を組み合わせることで簡単にグラフを並べたり重ねたりできます。

0

bokeh 2.4の場合、bokeh.plotting.Figure.varea_stack関数で、積み上げ面グラフを作ることができました。

https://docs.bokeh.org/en/latest/docs/reference/plotting/figure.html#bokeh.plotting.Figure.varea_stack

https://docs.bokeh.org/en/latest/docs/gallery/stacked_area.html

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。