要利用Bokeh制作堆疊條形圖,首先需要安裝Bokeh庫。然后可以按照以下步驟進行操作:
from bokeh.io import output_file, show
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource
data = {
'categories': ['A', 'B', 'C'],
'values1': [10, 20, 30],
'values2': [15, 25, 35]
}
source = ColumnDataSource(data=data)
p = figure(x_range=data['categories'], plot_height=350, title='Stacked Bar Chart', toolbar_location=None, tools="")
p.vbar(x='categories', top='values1', width=0.5, source=source, color='blue', legend_label='Values1')
p.vbar(x='categories', top='values2', width=0.5, source=source, color='red', legend_label='Values2')
p.y_range.start = 0
p.xgrid.grid_line_color = None
p.axis.minor_tick_line_color = None
p.outline_line_color = None
p.legend.location = "top_left"
p.legend.orientation = "horizontal"
output_file("stacked_bar_chart.html")
show(p)
通過以上步驟,您可以利用Bokeh輕松制作堆疊條形圖,并將其保存為HTML文件展示出來。