python - Bokeh 直方图不会绘制

标签 python pandas histogram bokeh

我的问题与这篇文章完全一样(尽管列类型可能不同):

Cannot plot Histogram on Ubuntu 14.04

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

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

我的系统是SUSE。只是试图从 pandas 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]')

如有任何帮助,我们将不胜感激。

干杯,谢谢

最佳答案

不要使用 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)

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,请查看以下选项:

关于python - Bokeh 直方图不会绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45807960/

相关文章:

python - MAC OS X Pebble SDK 3.0 构建错误 : Compilation error InverterLayer

python - 如何计算列中使用的前 3 个单词并将结果存储在字典中

gnuplot - 在 gnuplot 中标准化直方图箱

image-processing - 从直方图中检测两个最高峰

python - 从 iterable 实现 django OR 查询的最佳实践方法?

python - 访问 pandas 数据框中的列时出现问题

python - numpy 反向多维数组

python - 如何在没有迭代的情况下基于 Pandas 中的 2+ 个条件创建一个新的 df.column?

python - Aggfunc 的 Pandas 数据透视表列表

python - matplotlib 中的 100% 堆积面积/直方图,X 轴上有日期