散景中columnDataSource的用途

rpj*_*rpj 16 bokeh

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

mul*_*rse 13

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

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

  • 您能否添加一个小例子来说明如何执行您在答案中所写的内容?说出您希望将鼠标悬停在图表上方时查看数据的时间序列 (2认同)

amu*_*lly 6

这应该有效:

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à.
Run Code Online (Sandbox Code Playgroud)

  • 嘿凯尔,感谢您的关注。老实说,我的答案是一个情节的例子,而不是对数据类型的解释。我认为一些早期的答案在解释这个理论方面做得很好。我的目的是添加一个简短的、实际的例子,正如前面@famargar 所要求的那样。 (4认同)