可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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