【问题标题】:Cannot plot Histogram on Ubuntu 14.04无法在 Ubuntu 14.04 上绘制直方图
【发布时间】:2017-01-27 05:16:41
【问题描述】:

我在 Ubuntu 14.04 上使用 Python 2.7 和 Bokeh 0.12.4。我有一个像这样的数据框:

         msrp  price
compact   1.0    1.0
sedan     2.0    3.0
suv       3.0    5.0
sport     4.0    7.0

这样做:

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

s = pd.Series([1,2,3,4], index=['compact', 'sedan', 'suv', 'sport'], dtype='float64')
s2 = pd.Series([1,3,5,7], index=['compact', 'sedan', 'suv', 'sport'], dtype='float64')
df = pd.DataFrame({'msrp': s, 'price': s2})

output_file('test.html')
p = Histogram(df['msrp'], title='Test')
show(p)

当我运行它时,我收到以下错误:

ValueError: expected an element of either Column(Float), Column(Int), Column(String), Column(Date), Column(Datetime) or Column(Bool), got 0    2
dtype: int64

这令人费解,因为当我检查 msrp 系列时,我得到:

>>> df['msrp']
compact    1.0
sedan      2.0
suv        3.0
sport      4.0
Name: msrp, dtype: float64

请注意,dtype 读取为浮点数。我究竟做错了什么?我应该注意所有其他图表类型都可以正常工作。

更新 文档上的示例也不起作用:

from bokeh.sampledata.autompg import autompg as df
p = Histogram(df['hp'], title='Test')

同样的错误。这是一个已知的问题?如果是这样,文档应该更新...

更新

我在 Macbook 上没有这个问题。只有 Ubuntu。 Bokeh 和 Linux 之间是否存在兼容性问题?我在使用 Bokeh 0.12.4、0.12.3 和 0.11.0 时遇到了这个问题。

【问题讨论】:

  • 以上所有代码都为我工作。也许你有安装问题。
  • 我清除然后重新安装,同样的问题。 bokeh 和 Ubuntu 之间是否存在兼容性问题?

标签: python ubuntu data-visualization bokeh


【解决方案1】:

旧的bokeh.charts API(包括Histogram)已被弃用并随后被删除。要使用 Bokeh 创建直方图,您应该使用 bokeh.plotting API。有多种方法可以使用,这里有一个完整的示例,使用 Bokeh 0.13 创建:

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

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

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

show(p)

【讨论】:

    猜你喜欢
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-23
    • 2016-03-24
    • 2014-10-05
    • 2014-08-08
    • 2016-02-16
    相关资源
    最近更新 更多