Plotting Bar Charts with Bokeh

馋奶兔 提交于 2019-12-10 10:37:29

问题


I am trying to plot a basic barchart but I keep seeing an error called 'StopIteration'. I am following an example, and the code seems fine:

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()

回答1:


UPDATE this answer is out of date and will not work with Bokeh versions newer than 0.10

Please refer to recent documentation



You're passing invalid input. From the doc:

(dict, OrderedDict, lists, arrays and DataFrames are valid inputs)

This is the example they have on there:

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)

Your amount is a dict_values() which will not be accepted. I'm not sure what your bugrlary_dict is but have that as the data for the Bar() and I'm assuming your months is the label. That should work assuming len(bugrlary_dict) == 12

Output from Bokeh's example:




回答2:


In Bokeh 0.12.5, you can do it the following way:

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)

Output:

Change the parameter in Bar() from group to stack if you want a stacked bar chart



来源:https://stackoverflow.com/questions/33469662/plotting-bar-charts-with-bokeh

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!