python Bokeh交互式可视化

📚 简介

Bokeh是一个Python交互式可视化库,支持Web浏览器展示。

🚀 堆叠柱状图示例

from bokeh.palettes import HighContrast3
from bokeh.plotting import figure, show

fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
years = ["2015", "2016", "2017"]

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

p = figure(
    x_range=fruits, 
    height=250, 
    title="Fruit Counts by Year",
    toolbar_location=None, 
    tools="hover", 
    tooltips="$name @fruits: @$name"
)

p.vbar_stack(
    years, 
    x='fruits', 
    width=0.9, 
    color=HighContrast3, 
    source=data,
    legend_label=years
)

p.y_range.start = 0
p.x_range.range_padding = 0.1
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"

show(p)

📦 主要特性

  • ✅ 交互式图表
  • ✅ 支持Web展示
  • ✅ 多种图表类型
  • ✅ 支持大数据集

📚 参考资源


作者:spike

分类: Python

创作时间:2026-02-23

更新时间:2026-02-23