matplotlib でステップ関数を作成するには、次のように記述します。
import matplotlib.pyplot as plt
x = [1,2,3,4]
y = [0.002871972681775004, 0.00514787917410944,
0.00863476098280219, 0.012003316194034325]
plt.step(x, y)
plt.show()
Bokeh で同様のグラフを作成するにはどうすればよいですか?
Bokeh には、Step
バージョン のグリフが組み込まれてい0.12.11
ます。
from bokeh.plotting import figure, output_file, show
output_file("line.html")
p = figure(plot_width=400, plot_height=400)
# add a steps renderer
p.step([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], line_width=2, mode="center")
show(p)
line
配列を手動で操作することで、それを行うことができます。軸のリストのインターリーブに注意してください。y
また、右と左の制限を達成するために、x 値をわずかに変更したい (と思います)。次のものを提供させてください。
from bokeh.plotting import figure
import numpy as np
f = figure()
#
# Example vectors (given):
x = [1,2,3,4]
y = [0.002871972681775004, 0.00514787917410944,
0.00863476098280219, 0.012003316194034325]
#
_midVals = np.diff(x)/2.0
xs = set(x[:-1]-_midVals).union(x[1:]+_midVals)
xl = list(xs)
xl.sort()
xx = xl[1:]+xl[:-1]
xx.sort()
yy = y+y
yy[::2] = y
yy[1::2] = y
#
# assert/print coordinates
assert len(xx)==len(yy)
for _p in zip(xx,yy):
print _p
#
# Plot!
f.line(x=xx, y=yy)
show(f)
「健全性チェック」プリントの出口は次のようになります。
(0.5, 0.002871972681775004)
(1.5, 0.002871972681775004)
(1.5, 0.00514787917410944)
(2.5, 0.00514787917410944)
(2.5, 0.00863476098280219)
(3.5, 0.00863476098280219)
(3.5, 0.012003316194034325)
(4.5, 0.012003316194034325)
そしてプロット:
ここに到着した人の助けになることを願っています。