Bokeh

如何在Bokeh中创建分组或嵌套的条形图

小樊
82
2024-05-20 11:39:35
栏目: 编程语言

要在Bokeh中创建分组或嵌套的条形图,可以通过使用vbar函数来实现。以下是一个示例代码,演示如何创建一个分组的条形图:

from bokeh.plotting import figure, show
from bokeh.io import output_notebook

output_notebook()

fruits = ['Apples', 'Oranges', 'Bananas']
years = ['2015', '2016', '2017']

data = {'fruits' : fruits,
        '2015'   : [2, 1, 4],
        '2016'   : [5, 3, 2],
        '2017'   : [3, 2, 5]}

p = figure(x_range=fruits, plot_height=250, title="Fruit Counts by Year",
           toolbar_location=None, tools="")

p.vbar(x='fruits', top='2015', width=0.2, source=data, color="red", legend_label="2015")
p.vbar(x='fruits', top='2016', width=0.2, source=data, color="blue", legend_label="2016", alpha=0.5)
p.vbar(x='fruits', top='2017', width=0.2, source=data, color="green", legend_label="2017")

p.xgrid.grid_line_color = None
p.y_range.start = 0
p.y_range.end = 10
p.legend.location = "top_left"
p.legend.orientation = "horizontal"

show(p)

这段代码将创建一个简单的分组条形图,其中每个水果(苹果,橙子和香蕉)在2015年,2016年和2017年的数量将以不同的颜色显示。可以根据需要修改颜色、宽度和其他参数来定制图表。

0
看了该问题的人还看了