散景

时间:2018-04-11 07:32:27

标签: python bokeh multi-level valueerror

我正在尝试使用散景来执行一些条形图绘制,这对于向用户呈现数据非常有用。散景用户指南网站中的多级条形图类别看起来很棒,并且直接符合我的需求。

但是,我想开始使用他们提供的代码作为示例,看看它是如何工作的以及我可以改变的。我遇到的问题是我甚至无法运行他们的示例代码。

他们提供的用于创建多级分类条形图的示例代码是:

from bokeh.io import show, output_file
from bokeh.models import ColumnDataSource, FactorRange
from bokeh.plotting import figure

output_file("bars.html")

fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
years = ['2015', '2016', '2017']

data = {'fruits' : fruits,
        '2015'   : [2, 1, 4, 3, 2, 4],
        '2016'   : [5, 3, 3, 2, 4, 6],
        '2017'   : [3, 2, 4, 4, 5, 3]}

# this creates [ ("Apples", "2015"), ("Apples", "2016"), ("Apples", "2017"), ("Pears", "2015), ... ]
x = [ (fruit, year) for fruit in fruits for year in years ]
counts = sum(zip(data['2015'], data['2016'], data['2017']), ()) # like an hstack

source = ColumnDataSource(data=dict(x=x, counts=counts))

p = figure(x_range=FactorRange(*x), plot_height=250, title="Fruit Counts by Year",
           toolbar_location=None, tools="")

p.vbar(x='x', top='counts', width=0.9, source=source)

p.y_range.start = 0
p.x_range.range_padding = 0.1
p.xaxis.major_label_orientation = 1
p.xgrid.grid_line_color = None

show(p)

但是,运行此代码会从FactorRange类返回ValueError

ValueError: expected an element of either List(String) or List(Int), got [('Apples', '2015'), ('Apples', '2016'), ('Apples', '2017'), ('Pears', '2015'), ('Pears', '2016'), ('Pears', '2017'), ('Nectarines', '2015'), ('Nectarines', '2016'), ('Nectarines', '2017'), ('Plums', '2015'), ('Plums', '2016'), ('Plums', '2017'), ('Grapes', '2015'), ('Grapes', '2016'), ('Grapes', '2017'), ('Strawberries', '2015'), ('Strawberries', '2016'), ('Strawberries', '2017')]

问题是:我错过了什么吗?是否有我没有的更新或我需要导入的模块才能使其工作?

提前致谢。

0 个答案:

没有答案