使用 Bokeh 绘制条形图
Plotting Bar Charts with 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()
UPDATE 这个答案已经过时,不适用于比 0.10
您传递的输入无效。来自 doc:
(dict, OrderedDict, lists, arrays and DataFrames are valid inputs)
这是他们在那里的例子:
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)
您的 amount
是 dict_values()
,将不会被接受。我不确定您的 bugrlary_dict
是什么,但将其作为 Bar()
的 data
,我假设您的 months
是标签。假设 len(bugrlary_dict) == 12
应该可以工作
散景示例的输出:
在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)
输出:
如果您想要堆叠条形图,请将 Bar()
中的参数从 group
更改为 stack
我正在尝试绘制基本条形图,但我一直看到名为 '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()
UPDATE 这个答案已经过时,不适用于比 0.10
您传递的输入无效。来自 doc:
(dict, OrderedDict, lists, arrays and DataFrames are valid inputs)
这是他们在那里的例子:
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)
您的 amount
是 dict_values()
,将不会被接受。我不确定您的 bugrlary_dict
是什么,但将其作为 Bar()
的 data
,我假设您的 months
是标签。假设 len(bugrlary_dict) == 12
散景示例的输出:
在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)
输出:
如果您想要堆叠条形图,请将 Bar()
中的参数从 group
更改为 stack