Bokeh直方图不会绘图,是否有解决这个老问题的方法

时间:2017-08-22 02:20:40

标签: python pandas histogram bokeh

我的问题看起来与这篇文章完全相同(尽管列类型可能不同):

Cannot plot Histogram on Ubuntu 14.04

代码直接来自文档http://bokeh.pydata.org/en/0.10.0/docs/user_guide/charts.html#histograms

我无法评论该帖子,因此需要再次询问是否找到了解决方案......

我的系统是SUSE。只是试图从熊猫df系列中绘制一个简单的日期时间直方图。

>>>df
           ACQ_DATE
0       2017-01-28
1       2017-01-28
...            ...
456365  2017-07-25
456366  2017-07-25

>>>hist = Histogram(df['ACQ_DATE'], title="Fire Frequency")

2017-08-22 11:56:15,240 Error running application handler <bokeh.application.handlers.script.ScriptHandler object at 0x2b6cc2c8f358>:     expected an element of either Column(Float) or Column(String), got array(['2017-    01-28T00:00:00.000000000', '2017-01-28T00:00:00.000000000',
   '2017-01-28T00:00:00.000000000', ...,
   '2017-07-25T00:00:00.000000000', '2017-07-25T00:00:00.000000000',
   '2017-07-25T00:00:00.000000000'], dtype='datetime64[ns]')
File "properties.py", line 676, in validate:
raise ValueError("expected an element of either %s, got %r" % (nice_join    (self.type_params), value)) Traceback (most recent call last):
  File "/home/byed/venv36/lib/python3.6/site-    packages/bokeh/application/handlers/code_runner.py", line 81, in run
exec(self._code, module.__dict__)
  File "/home/byed/job/fire/report_fire_points.py", line 118, in <module>
    hist = Histogram(df['ACQ_DATE'], title="Fire Frequency")  #,     tools='pan,wheel_zoom,box_select,reset')
  File "/home/byed/venv36/lib/python3.6/site-    packages/bkcharts/builders/histogram_builder.py", line 107, in Histogram
    return create_and_build(HistogramBuilder, data, **kw)
  File "/home/byed/venv36/lib/python3.6/site-packages/bkcharts/builder.py", line 56, in create_and_build
    chart.add_builder(builder)
  File "/home/byed/venv36/lib/python3.6/site-packages/bkcharts/chart.py", line 155, in add_builder
    builder.create(self)
  File "/home/byed/venv36/lib/python3.6/site-packages/bkcharts/builder.py", line 512, in create
    chart.add_renderers(self, renderers)
  File "/home/byed/venv36/lib/python3.6/site-packages/bkcharts/chart.py", line 150, in add_renderers
    self.renderers += renderers
  File "/home/byed/venv36/lib/python3.6/site-packages/bokeh/core/property/containers.py", line 76, in wrapper
    result = func(self, *args, **kwargs)
  File "/home/byed/venv36/lib/python3.6/site-    packages/bokeh/core/property/containers.py", line 172, in __iadd__
    return super(PropertyValueList, self).__iadd__(y)
  File "/home/byed/venv36/lib/python3.6/site-   packages/bkcharts/builders/bar_builder.py", line 221, in yield_renderers
    **group_kwargs)
  File "/home/byed/venv36/lib/python3.6/site-packages/bkcharts/glyphs.py", line 950, in __init__
    super(HistogramGlyph, self).__init__(**kwargs)
  File "/home/byed/venv36/lib/python3.6/site-packages/bkcharts/glyphs.py", line 490, in __init__
    super(AggregateGlyph, self).__init__(**kwargs)
  File "/home/byed/venv36/lib/python3.6/site-packages/bkcharts/models.py", line 83, in __init__
    super(CompositeGlyph, self).__init__(**properties)
  File "/home/byed/venv36/lib/python3.6/site-packages/bokeh/core/has_props.py", line 246, in __init__
    setattr(self, name, value)
  File "/home/byed/venv36/lib/python3.6/site- packages/bokeh/core/has_props.py", line 274, in __setattr__
    super(HasProps, self).__setattr__(name, value)
  File "/home/byed/venv36/lib/python3.6/site-   packages/bokeh/core/property/descriptors.py", line 495, in __set__
    self._internal_set(obj, value, setter)
  File "/home/byed/venv36/lib/python3.6/site-packages/bokeh/core/property/descriptors.py", line 713, in _internal_set
    value = self.property.prepare_value(obj, self.name, value)
  File "/home/byed/venv36/lib/python3.6/site-packages/bokeh/core/property/bases.py", line 290, in prepare_value
    raise e
  File "/home/byed/venv36/lib/python3.6/site-packages/bokeh/core/property/bases.py", line 283, in prepare_value
self.validate(value)
  File "/home/byed/venv36/lib/python3.6/site-packages/bokeh/core/properties.py", line 676, in validate
    raise ValueError("expected an element of either %s, got %r" % (nice_join   (self.type_params), value))
    ValueError: expected an element of either Column(Float) or Column(String),   got array(['2017-01-28T00:00:00.000000000', '2017-01-28T00:00:00.000000000',
   '2017-01-28T00:00:00.000000000', ...,
   '2017-07-25T00:00:00.000000000', '2017-07-25T00:00:00.000000000',
   '2017-07-25T00:00:00.000000000'], dtype='datetime64[ns]')

非常感谢任何帮助。

干杯谢谢

2 个答案:

答案 0 :(得分:2)

不要使用bokeh.charts(现在是一个单独的bkcharts项目),包括Histogrambkcharts项目目前放弃了未维护的。但是,使用bokeh.plotting创建直方图非常简单,这是Bokeh的稳定且良好支持的核心API:

import numpy as np
from bokeh.io import show, output_file
from bokeh.plotting import figure

data = np.random.normal(0, 0.5, 1000)
hist, edges = np.histogram(data, density=True, bins=50)

x = np.linspace(-2, 2, 1000)

p = figure()
p.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:], line_color="white")

output_file("hist.html")
show(p)

enter image description here

或者,如果您想在Bokeh之上使用非常高级别的统计图表API,那么请查看Holoviews:

http://holoviews.org/

Holoviews几乎在所有方面都优于bokeh.charts,更重要的是由活跃的团队维护。它被核心Bokeh开发团队正式认可为Bokeh未来的最佳和支持的非常高级的API。

答案 1 :(得分:0)

感谢@bigreddot!

我使用 Google Colab 作为在线 Jupyter 笔记本环境。为了在 Colab 中绘制散景直方图,我必须使用 output_notebook() 调整上面的示例代码片段:

import numpy as np
from bokeh.io import show, output_notebook
from bokeh.plotting import figure
output_notebook()

data = np.random.normal(0, 0.5, 1000)
hist, edges = np.histogram(data, density=True, bins=10)

p = figure()
p.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:], line_color="white")

show(p)