绘制条形图与散景

时间:2015-11-02 02:05:27

标签: python bokeh

我正在尝试绘制一个基本条形图,但我一直看到一个名为“StopIteration”的错误。我正在关注一个例子,代码似乎很好:

amount = bugrlary_dict.values()
months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]

print len(amount)
print len(months)
bar = Bar(amount, months, filename="bar.html")

bar.title("Bar Chart of the Amount of Burglaries").xlabel("Months").ylabel("Amount")
bar.show()

2 个答案:

答案 0 :(得分:2)

更新 此答案已过期,不适用于比0.10

更新的Bokeh版本

请参阅recent documentation

您正在传递无效输入。来自doc

  

(dict,OrderedDict,lists,arrays和DataFrames是有效输入)

这是他们在那里的例子:

from collections import OrderedDict
from bokeh.charts import Bar, output_file, show

# (dict, OrderedDict, lists, arrays and DataFrames are valid inputs)
xyvalues = OrderedDict()
xyvalues['python']=[-2, 5]
xyvalues['pypy']=[12, 40]
xyvalues['jython']=[22, 30]

cat = ['1st', '2nd']

bar = Bar(xyvalues, cat, title="Stacked bars",
        xlabel="category", ylabel="language")

output_file("stacked_bar.html")
show(bar)

您的amountdict_values(),我们将不予受理。我不确定您的bugrlary_dict是什么,但将其作为data的{​​{1}},我假设您的Bar()是标签。这应该假设months

Bokeh的例子输出:

enter image description here

答案 1 :(得分:1)

Bokeh 0.12.5中,您可以通过以下方式执行此操作:

from bokeh.charts import Bar, output_file, show

# best support is with data in a format that is table-like
data = {
    'sample': ['1st', '2nd', '1st', '2nd', '1st', '2nd'],
    'interpreter': ['python', 'python', 'pypy', 'pypy', 'jython', 'jython'],
    'timing': [-2, 5, 12, 40, 22, 30]
}

# x-axis labels pulled from the interpreter column, grouping labels from sample column
bar = Bar(data, values='timing', label='sample', group='interpreter',
      title="Python Interpreter Sampling - Grouped Bar chart",
      legend='top_left', plot_width=400, xlabel="Category", ylabel="Language")

output_file("grouped_bar.html")
show(bar)

输出:

enter image description here

如果您需要堆积条形图,请将Bar()中的参数从group更改为stack