Bokeh热力图的使用方法

7

我有一组带时间戳的事件数组,想要创建热力图:

  • x轴应该表示日期,例如“2016-02-03”,
  • y轴应该表示发生的小时数,例如13(如果是13:32),
  • 颜色应该取决于发生事件的数量。

我的数据(作为pandas dataframe z.head()):

         date hour  i
0  2016-01-15   13  1
1  2016-01-15   13  1
2  2016-01-15   12  1
3  2016-01-15   10  1
4  2016-01-15   10  1

我的失败尝试:

from bokeh._legacy_charts import HeatMap, output_file, show
hm = HeatMap(z.head(), x='date', y='hour', values='i', stat='count')

还有一个例外:

AttributeError                            Traceback (most recent call last)
<ipython-input-98-83c7e9319496> in <module>()
----> 1 hm = HeatMap(z.head(), x='date', y='hour', values='i', stat='count')

/home/user/.local/lib/python3.4/site-packages/bokeh/charts/builders/heatmap_builder.py in HeatMap(data, x, y, values, stat, xgrid, ygrid, hover_tool, hover_text, **kw)
     90     kw['values'] = values
     91     kw['stat'] = stat
---> 92     chart = create_and_build(HeatMapBuilder, data, xgrid=xgrid, ygrid=ygrid, **kw)
     93 
     94     if hover_tool:

/home/user/.local/lib/python3.4/site-packages/bokeh/charts/builder.py in create_and_build(builder_class, *data, **kws)
     65     chart_kws = { k:v for k,v in kws.items() if k not in builder_props}
     66     chart = Chart(**chart_kws)
---> 67     chart.add_builder(builder)
     68     chart.start_plot()
     69 

/home/user/.local/lib/python3.4/site-packages/bokeh/charts/chart.py in add_builder(self, builder)
    150     def add_builder(self, builder):
    151         self._builders.append(builder)
--> 152         builder.create(self)
    153 
    154     def add_ranges(self, dim, range):

/home/user/.local/lib/python3.4/site-packages/bokeh/charts/builder.
    510         # call methods that allow customized setup by subc
    511         self.setup()
--> 512         self.process_data()
    513 
    514         # create and add renderers to chart

/home/user/.local/lib/python3.4/site-packages/bokeh/charts/builders
    169         # color by the values selection
    170         self.attributes['color'].setup(data=self._data.sou
--> 171                                        columns=self.values
    172         self.attributes['color'].add_bin_labels(self._data
    173 

/home/user/.local/lib/python3.4/site-packages/bokeh/charts/attribut
    187 
    188         if self.columns is not None and self.data is not N
--> 189             self.attr_map = self._create_attr_map(self.dat
    190 
    191     def update_data(self, data):

/home/user/.local/lib/python3.4/site-packages/bokeh/charts/attribut
    158         """Creates map between unique values and available
    159 
--> 160         self._generate_items(df, columns)
    161         iterable = self._setup_iterable()
    162 

/home/user/.local/lib/python3.4/site-packages/bokeh/charts/attribut
    230 
    231                 self.bins = Bins(source=ColumnDataSource(d
--> 232                                  bin_count=len(self.iterab
    233 
    234                 if self.sort:

/home/user/.local/lib/python3.4/site-packages/bokeh/charts/stats.py
    309         properties['source'] = source
    310 
--> 311         super(Bins, self).__init__(**properties)
    312 
    313     def _get_stat(self):

/home/user/.local/lib/python3.4/site-packages/bokeh/charts/stats.py
     54 
     55         super(Stat, self).__init__(**properties)
---> 56         self._refresh()
     57 
     58     def _refresh(self):

/home/user/.local/lib/python3.4/site-packages/bokeh/charts/stats.py
     60         if self.get_data() is not None:
     61             self.update()
---> 62             self.calculate()
     63 
     64     def set_data(self, data, column=None):

/home/user/.local/lib/python3.4/site-packages/bokeh/charts/stats.py
    342         if self.source is not None:
    343             # add bin column to data source
--> 344             self.source.add(binned.tolist(), name=self.bin
    345             df = self.source.to_df()
    346         else:

AttributeError: 'Categorical' object has no attribute 'tolist'

Bokeh是在0.11版本中通过pip3安装的。我做错了什么?谢谢。

更新:

  • 我的legacy_charts写错了(复制了错误的行)。
  • 我已经重新安装了Bokeh pip3 install --user --force-reinstall --upgrade bokeh。仍然没有成功。

完整代码:

import pandas as pd
from bokeh.charts import HeatMap, output_file, show

z = pd.DataFrame()
z['date'] = ['2016-01-15', '2016-01-13', '2016-01-11', '2016-01-14', '2016-01-15']
z['hour'] = [12, 10, 11, 3, 0]
z['i'] = [1, 1, 1, 1, 1]

output_file('/tmp/test.html')
hm = HeatMap(z, x='date', y='hour', stat='count')
show(hm)

并且使用Python3运行:

Traceback (most recent call last):
  File "so.py", line 10, in <module>
    hm = HeatMap(z, x='date', y='hour', stat='count')
  File "/home/ktx/.local/lib/python3.4/site-packages/bokeh/charts/builders/heatmap_builder.py", line 92, in HeatMap
    chart = create_and_build(HeatMapBuilder, data, xgrid=xgrid, ygrid=ygrid, **kw)
  File "/home/ktx/.local/lib/python3.4/site-packages/bokeh/charts/builder.py", line 67, in create_and_build
    chart.add_builder(builder)
  File "/home/ktx/.local/lib/python3.4/site-packages/bokeh/charts/chart.py", line 152, in add_builder
    builder.create(self)
  File "/home/ktx/.local/lib/python3.4/site-packages/bokeh/charts/builder.py", line 512, in create
    self.process_data()
  File "/home/ktx/.local/lib/python3.4/site-packages/bokeh/charts/builders/heatmap_builder.py", line 171, in process_data
    columns=self.values.selection)
  File "/home/ktx/.local/lib/python3.4/site-packages/bokeh/charts/attributes.py", line 189, in setup
    self.attr_map = self._create_attr_map(self.data.to_df(), self.columns)
  File "/home/ktx/.local/lib/python3.4/site-packages/bokeh/charts/attributes.py", line 160, in _create_attr_map
    self._generate_items(df, columns)
  File "/home/ktx/.local/lib/python3.4/site-packages/bokeh/charts/attributes.py", line 232, in _generate_items
    bin_count=len(self.iterable), aggregate=False)
  File "/home/ktx/.local/lib/python3.4/site-packages/bokeh/charts/stats.py", line 311, in __init__
    super(Bins, self).__init__(**properties)
  File "/home/ktx/.local/lib/python3.4/site-packages/bokeh/charts/stats.py", line 56, in __init__
    self._refresh()
  File "/home/ktx/.local/lib/python3.4/site-packages/bokeh/charts/stats.py", line 62, in _refresh
    self.calculate()
  File "/home/ktx/.local/lib/python3.4/site-packages/bokeh/charts/stats.py", line 344, in calculate
    self.source.add(binned.tolist(), name=self.bin_column)
AttributeError: 'Categorical' object has no attribute 'tolist'

1
我只能猜测您可能存在安装问题。bokeh._legacy_charts已被删除,在Bokeh版本0.11中不存在。也许尝试完全删除并重新安装。 - bigreddot
@bigreddot 你说得对,我在复制粘贴到SO时犯了错误。我发布了整个代码,包括示例数据创建和来自python3的错误。 - ktx
1
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - bigreddot
2个回答

2

bigreddot说得对。如果你的版本是0.11,你就不应该有legacy_charts。一旦你正确地更新了Bokeh,这个问题就应该解决了:

from bokeh.charts import HeatMap, output_file, show
import pandas as pd

output_file('test.html')

hm = HeatMap(z, x='date', y='hour',  values='i', stat='count')

show(hm)

enter image description here


1
  1. 在 bigreddot 指针上,我重新安装了 bokeh,但仍然无法使用。我在 pip3 中多次遇到错误(一些与 Linux 发行版级别有关的 bug)。
  2. 然后 python3-pip 包、pandas 和其他几个包被清除了。
  3. 之后执行了 rm -rf ~/.local/lib/python* 来清理用户安装目录。
  4. 然后下载了 get-pip.py
  5. 我重新安装了 pip3 (+ ln -s ~/.local/bin/pip3 ~/bin) 并从 PyPi 存储库重新安装了 bokeh & pandas。

现在一切都很好。感谢 bigreddot 指引我方向。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接