Bokeh-axes

提供:Dev Guides
移動先:案内検索

ボケ-軸

この章では、さまざまなタイプの軸について説明します。

Sr.No Axes Description
1 Categorical Axes The bokeh plots show numerical data along both x and y axes. In order to use categorical data along either of axes, we need to specify a FactorRange to specify categorical dimensions for one of them.
2 Log Scale Axes If there exists a power law relationship between x and y data series, it is desirable to use log scales on both axes.
3 Twin Axes It may be needed to show multiple axes representing varying ranges on a single plot figure. The figure object can be so configured by defining extra_x_range *and extra_y_range* properties

カテゴリカル軸

これまでの例では、ボケプロットはx軸とy軸の両方に沿った数値データを示しています。 いずれかの軸に沿ってカテゴリカルデータを使用するには、いずれかの軸のカテゴリカルディメンションを指定するFactorRangeを指定する必要があります。 たとえば、x軸の指定されたリストで文字列を使用するには-

langs = ['C', 'C++', 'Java', 'Python', 'PHP']
fig = figure(x_range = langs, plot_width = 300, plot_height = 300)

次の例では、提供されるさまざまなコースに登録されている学生の数を示す簡単な棒グラフが表示されます。

from bokeh.plotting import figure, output_file, show
langs = ['C', 'C++', 'Java', 'Python', 'PHP']
students = [23,17,35,29,12]
fig = figure(x_range = langs, plot_width = 300, plot_height = 300)
fig.vbar(x = langs, top = students, width = 0.5)
show(fig)

出力

カテゴリ軸

各バーを異なる色で表示するには、vbar()関数のcolorプロパティをカラー値のリストに設定します。

cols = ['red','green','orange','navy', 'cyan']
fig.vbar(x = langs, top = students, color = cols,width=0.5)

出力

plot

vbar_stack()またはhbar_stack()関数を使用して垂直(または水平)積み上げ棒をレンダリングするには、stackersプロパティを連続してスタックするフィールドのリストに設定し、sourceプロパティを各フィールドに対応する値を含むdictオブジェクトに設定します。

次の例のsalesは、3か月間の3つの製品の売上高を示す辞書です。

from bokeh.plotting import figure, output_file, show
products = ['computer','mobile','printer']
months = ['Jan','Feb','Mar']
sales = {'products':products,
   'Jan':[10,40,5],
   'Feb':[8,45,10],
   'Mar':[25,60,22]}
cols = ['red','green','blue']#,'navy', 'cyan']
fig = figure(x_range = products, plot_width = 300, plot_height = 300)
fig.vbar_stack(months, x = 'products', source = sales, color = cols,width = 0.5)
show(fig)

出力

販売辞書

グループ化された棒グラフは、 bokeh.transform モジュールのdodge()関数を使用して棒の視覚的な変位を指定することで取得されます。

  • dodge()関数*は、各棒グラフに相対オフセットを導入し、それによってグループの視覚的な印象を実現します。 次の例では、* vbar()グリフ*は、特定の月のバーのグループごとに0.25のオフセットで区切られています。
from bokeh.plotting import figure, output_file, show
from bokeh.transform import dodge
products = ['computer','mobile','printer']
months = ['Jan','Feb','Mar']
sales = {'products':products,
   'Jan':[10,40,5],
   'Feb':[8,45,10],
   'Mar':[25,60,22]}
fig = figure(x_range = products, plot_width = 300, plot_height = 300)
fig.vbar(x = dodge('products', -0.25, range = fig.x_range), top = 'Jan',
   width = 0.2,source = sales, color = "red")
fig.vbar(x = dodge('products', 0.0, range = fig.x_range), top = 'Feb',
   width = 0.2, source = sales,color = "green")
fig.vbar(x = dodge('products', 0.25, range = fig.x_range), top = 'Mar',
   width = 0.2,source = sales,color = "blue")
show(fig)

出力

視覚変位

対数目盛軸

プロットの軸の1つの値が、別の軸の線形に増加する値とともに指数関数的に増加する場合、以前の軸のデータを対数スケールで表示する必要があることがよくあります。 たとえば、xとyのデータ系列の間に指数法則の関係が存在する場合、両方の軸で対数スケールを使用することが望ましいです。

Bokeh.plotting APIのfigure()関数は、x_axis_typeおよびy_axis_typeを引数として受け入れ、これらのパラメーターのいずれかの値に「log」を渡すことにより、ログ軸として指定できます。

最初の図は、線形スケールでのxと10xの間のプロットを示しています。 2番目の図では、y_axis_typeは「log」に設定されています

from bokeh.plotting import figure, output_file, show
x = [0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]
y = [10**i for i in x]
fig = figure(title = 'Linear scale example',plot_width = 400, plot_height = 400)
fig.line(x, y, line_width = 2)
show(fig)

出力

Log Scale Axes

ここで、figure()関数を変更して、y_axis_type = ’log’を構成します。

fig = figure(title = 'Linear scale example',plot_width = 400, plot_height = 400, y_axis_type = "log")

出力

線形スケール

ツインアクス

特定の状況では、1つのプロット図でさまざまな範囲を表す複数の軸を表示する必要がある場合があります。 extra_x_range および extra_y_range プロパティを定義することで、figureオブジェクトをそのように構成できます。 図に新しいグリフを追加するとき、これらの名前付き範囲が使用されます。

正弦曲線と直線を同じプロットに表示しようとします。 両方のグリフには、異なる範囲のy軸があります。 サインカーブとラインのxとyのデータ系列は次のようにして得られます-

from numpy import pi, arange, sin, linspace
x = arange(-2*pi, 2*pi, 0.1)
y = sin(x)
y2 = linspace(0, 100, len(y))

ここで、xとyの間のプロットは正弦関係を表し、xとy2の間のプロットは直線です。 図オブジェクトは明示的なy_rangeで定義され、正弦曲線を表す線グリフが次のように追加されます-

fig = figure(title = 'Twin Axis Example', y_range = (-1.1, 1.1))
fig.line(x, y, color = "red")

追加のy範囲が必要です。 それは次のように定義されます-

fig.extra_y_ranges = {"y2": Range1d(start = 0, end = 100)}

右側にy軸を追加するには、add_layout()メソッドを使用します。 xとy2を表す新しい線記号を図に追加します。

fig.add_layout(LinearAxis(y_range_name = "y2"), 'right')
fig.line(x, y2, color = "blue", y_range_name = "y2")

これにより、2つのy軸を持つプロットが作成されます。 完全なコードと出力は次のとおりです-

from numpy import pi, arange, sin, linspace
x = arange(-2*pi, 2*pi, 0.1)
y = sin(x)
y2 = linspace(0, 100, len(y))
from bokeh.plotting import output_file, figure, show
from bokeh.models import LinearAxis, Range1d
fig = figure(title='Twin Axis Example', y_range = (-1.1, 1.1))
fig.line(x, y, color = "red")
fig.extra_y_ranges = {"y2": Range1d(start = 0, end = 100)}
fig.add_layout(LinearAxis(y_range_name = "y2"), 'right')
fig.line(x, y2, color = "blue", y_range_name = "y2")
show(fig)

出力

ツインアックス