用Bokeh绘制条形图

2024-10-01 15:30:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图绘制一个基本的条形图,但是我一直看到一个错误,叫做“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()

Tags: of代码len错误绘制baramount例子
2条回答

更新此答案已过时,无法与更新0.10的Bokeh版本一起使用

请参考recent documentation



您传递的输入无效。从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示例的输出:

enter image description here

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

相关问题 更多 >

    热门问题