2016-04-19 16 views
6

私はbokehを初めて使用しており、columnDataSourceの機能を把握しようとしています。それは多くの場所に現れますが、私はその目的とその仕組みが不明です。誰かを照らすことができますか?これが愚かな質問である場合のお詫び...bokehのcolumnDataSourceの目的

+2

を使用すると、Rまたはパンダ 'DataFrame'オブジェクトに精通している場合は、' ColumnDataSource'は、基本的にその簡単なバージョンです。これは、名前で参照できるデータ(列)の配列の集合です。実際の内部構造は、文字列をリスト/配列にマップする辞書です。データがPythonからBokehJSブラウザー・ライブラリーに移動する主な方法です。 – bigreddot

答えて

4

ColumnDataSourceは、Bokehグラフのデータが格納されるオブジェクトです。 ColumnDataSourceを使用せず、Python辞書、pandasデータフレームなどで直接グラフを表示することもできますが、グリフ上にマウスを置いたときにデータ情報を示すポップアップウィンドウを表示するなどの機能では、 ColumnDataSourceでなければ、ポップアップウィンドウはデータを取得できません。他の用途は、データをストリーミングするときである。

ディクショナリとパンダのデータフレームからColumnDataSourceを作成し、ColumnDataSourceを使用してグリフを作成できます。

+1

答えに書いたことをするための小さな例を追加してください。グラフの上にマウスを置いたときにデータを見たい時系列を言う – famargar

1

これは動作するはずです:

import pandas as pd 
import bokeh.plotting as bp 
from bokeh.models import HoverTool, DatetimeTickFormatter 

# Create the base data 
data_dict = {"Dates":["2017-03-01", 
        "2017-03-02", 
        "2017-03-03", 
        "2017-03-04", 
        "2017-03-05", 
        "2017-03-06"], 
      "Prices":[1, 2, 1, 2, 1, 2]} 

# Turn it into a dataframe 
data = pd.DataFrame(data_dict, columns = ['Dates', 'Prices']) 

# Convert the date column to the dateformat, and create a ToolTipDates column 
data['Dates'] = pd.to_datetime(data['Dates']) 
data['ToolTipDates'] = data.Dates.map(lambda x: x.strftime("%b %d")) # Saves work with the tooltip later 

# Create a ColumnDataSource object 
mySource = bp.ColumnDataSource(data) 

# Create your plot as a bokeh.figure object 
myPlot = bp.figure(height = 600, 
       width = 800, 
       x_axis_type = 'datetime', 
       title = 'ColumnDataSource', 
       y_range=(0,3)) 

# Format your x-axis as datetime. 
myPlot.xaxis[0].formatter = DatetimeTickFormatter(days='%b %d') 

# Draw the plot on your plot object, identifying the source as your Column Data Source object. 
myPlot.circle("Dates", 
      "Prices", 
      source=mySource, 
      color='red', 
      size = 25) 

# Add your tooltips 
myPlot.add_tools(HoverTool(tooltips= [("Dates","@ToolTipDates"), 
            ("Prices","@Prices")])) 


# Create an output file 
bp.output_file('columnDataSource.html', title = 'ColumnDataSource') 
bp.show(myPlot) # et voilà.