【问题标题】:Purpose of columnDataSource in bokeh散景中 columnDataSource 的用途
【发布时间】:2016-08-11 22:39:56
【问题描述】:

我是散景新手,想弄清楚 columnDataSource 的作用。它出现在很多地方,但我不确定它的目的和它是如何工作的。有人可以照亮吗?抱歉,如果这是一个愚蠢的问题...

【问题讨论】:

  • 如果您熟悉 R 或 Pandas DataFrame 对象,ColumnDataSource 基本上是一个更简单的版本。它是可以通过名称引用的数据数组(列)的集合。实际的内部结构就是:一个将字符串映射到列表/数组的字典。这是将数据从 python 移动到 BokehJS 浏览器库的主要方式。

标签: bokeh


【解决方案1】:

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

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

【讨论】:

  • 您能否添加一个关于如何执行您在答案中所写内容的小示例?将鼠标悬停在图表上方时,说出您希望查看数据的时间序列
【解决方案2】:

这应该可行:

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à.

【讨论】:

  • 这似乎是一个绘图示例,而不是数据类型的解释。
  • 嗨,凯尔,感谢您的关注。老实说,我的答案正是一个绘图示例,而不是数据类型的解释。我认为早期的一些答案很好地解释了这个理论。我的意图是按照@famargar 在线程前面的要求添加一个简短的实用示例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-07
  • 1970-01-01
相关资源
最近更新 更多