要利用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文件展示出来。