2016-12-12 5 views
2

ボケプロット上のx軸は、2016-01-01 12:00:00のような時間ではなく、five secondsのような時間を表します。私のBokeh x軸上のチックを適切にレンダリングする方法はありますか?ボケ0.12.6、you can use PrintfTickFormatterBokeh x_axis_typeの期間は?

enter image description here

+1

"おそらく、カスタムビジュアルフォーマッタを作成するべきです"(@bigreddotからgitter経由で)。おそらく誰かが答えとして模範デモンストレーションを提出する時間があります。 –

+0

この回答には例があります:http://stackoverflow.com/a/37182788/1736679 – Efren

答えて

1

:以下のプロットで0msの繰り返しから分かるようにx_axis_type='datetime'を設定することは、非常に正しいことをしません。

from bokeh.plotting import figure, output_file, show 
from bokeh.models import PrintfTickFormatter 

output_file('output.html') 

p = figure(plot_width=400, plot_height=400) p.line(x, y, size=1) 

# must be applied to the 1st element, not the axis itself 
p.xaxis[0].formatter = PrintfTickFormatter(format="%sms") 

show(p) 

あなたも、それも直線軸で動作します、x_axis_type='datetime'を設定する必要はありません。

編集:Bokehが現時点で処理するには洗練されていないため、単位のカスタム書式設定(ms/s/min、you have to use FuncTickFormatter)を適用します。それを0.12.6として使用するには2通りの方法があります。

最初に、transpilerを使用して、Python関数をFlexxpip install flexx)経由でJavascriptコードに変換します。これはすべてをPythonの構文の下に保ちますが、追加の依存関係を必要とします。

from bokeh.plotting import figure, output_file, show 
from bokeh.models import FuncTickFormatter 

output_file('output.html') 

p = figure(plot_width=400, plot_height=400) p.line(x, y, size=1) 

# custom formatter function 
def custom_formatter(): 
    units = [ 
     ('min', 60000.0), 
     ('s', 1000.0), 
     ('ms', 1.0), 
    ] 
    for u in units: 
     if tick >= u[1]: 
      return '{}{}'.format(tick/u[1], u[0]) 


# must be applied to the 1st element, not the axis itself 
p.xaxis[0].formatter = FuncTickFormatter.from_py_func(custom_formatter) 

show(p) 

最後に、実際のJavascriptコードを文字列として書き込み、パラメータとしてフォーマッタに渡すことで、 Bokehはそれをネイティブに行います。あなたはクライアント環境を制御できないので、純粋なバニラのJavascript以外のものを使用しないでください。

from bokeh.plotting import figure, output_file, show 
from bokeh.models import FuncTickFormatter 

output_file('output.html') 

p = figure(plot_width=400, plot_height=400) p.line(x, y, size=1) 

units = [ 
     ('min', 60000.0), 
     ('s', 1000.0), 
     ('ms', 1.0), 
    ] 

# must be applied to the 1st element, not the axis itself 
p.xaxis[0].formatter = FuncTickFormatter(code=""" var units = {'min': 
60000.0, 's': 1000.0, 'ms': 1.0}; for (u in units) { 
    if (tick >= units[u]) { 
     return (tick/units[u] + u); 
    } } """) 

show(p) 

私はそれが少し面倒だとわかりましたが、私はアプリケーションのために軸を固定しました。私はtickという変数をハードコードする必要があることを知っています。うまくいけば、Bokehは近い将来より良いソリューションを提供します。

+0

これは素晴らしいです。ミルク秒から数秒または数分に移動するときなど、日時単位を処理する方法に関する考えはありますか? – MRocklin

+0

@MRocklinはい、私のアプリケーションにも同様の問題がありました(ただし、日/月/年を扱います)。私が使用したソリューションを追加するための質問を編集しましたが、私が望んでいたよりもはるかに多くでした。 –