散景中columnDataSource的用途

时间:2016-04-19 16:01:41

标签: bokeh

我是bokeh的新手,并试图弄清楚columnDataSource的作用。它出现在很多地方,但我不确定它的目的和工作原理。有人可以照亮吗?如果这是一个愚蠢的问题,请道歉......

2 个答案:

答案 0 :(得分:10)

ColumnDataSource是存储散景图数据的对象。您可以选择不使用ColumnDataSource并使用Python词典,pandas数据帧等直接提供图形,但对于某些功能,例如当用户将鼠标悬停在字形上时显示数据信息的弹出窗口,您将被迫使用ColumnDataSource否则弹出窗口将无法获取数据。其他用途是流式数据。

您可以从字典和pandas数据帧创建ColumnDataSource,然后使用ColumnDataSource创建字形。

答案 1 :(得分: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à.