You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bokeh多级分类条形图官方示例代码无法运行问题咨询

Hey there! Let’s work through that frustrating issue with getting Bokeh’s multi-level categorical bar chart example up and running. I’ve hit similar roadblocks before, so here’s what to check and a full, working version of the code to get you started:

Common Fixes for Bokeh Multi-Level Bar Chart Issues

1. First, Check Your Dependencies & Bokeh Version

Outdated packages are the #1 culprit for these kinds of problems. Make sure you’ve got the latest stable Bokeh and pandas (most official examples rely on it) installed:

pip install --upgrade bokeh pandas

Older Bokeh versions (pre-2.0) had different syntax for FactorRange and categorical mappings, so upgrading will eliminate most compatibility bugs.

2. Use a Complete, Self-Contained Code Snippet

Sometimes the official docs skip small but critical bits (like imports or data setup) in their example snippets. Here’s a fully runnable version of the multi-level bar chart that matches the user guide example:

from bokeh.io import show, output_file
from bokeh.models import ColumnDataSource, FactorRange
from bokeh.plotting import figure
from bokeh.transform import factor_cmap
from bokeh.palettes import Viridis256

# Set up multi-category data
fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
years = ['2015', '2016', '2017']

# Build nested factor tuples for the x-axis
x = [(fruit, year) for fruit in fruits for year in years]
counts = [
    4, 5, 8, 12, 8, 9,
    2, 3, 4, 8, 7, 6,
    1, 2, 4, 4, 5, 4
]

# Create data source
source = ColumnDataSource(data=dict(x=x, counts=counts))

# Initialize plot with FactorRange
p = figure(
    x_range=FactorRange(*x), 
    height=350, 
    title="Fruit Counts by Year",
    toolbar_location=None, 
    tools=""
)

# Add vertical bars with color mapping
p.vbar(
    x='x', 
    top='counts', 
    width=0.9, 
    source=source,
    fill_color=factor_cmap(
        'x', 
        palette=Viridis256, 
        factors=years, 
        start=1, 
        end=2
    )
)

# Clean up the plot appearance
p.y_range.start = 0
p.x_range.range_padding = 0.1
p.xaxis.major_label_orientation = 1  # Rotate labels for readability
p.xgrid.grid_line_color = None

# Output to HTML (or just use show() to display instantly)
output_file("multi_level_bars.html")
show(p)

3. Fix Version-Specific Syntax Issues

If you’re stuck on an older Bokeh version (2.x instead of 3.x), adjust the factor_cmap line to remove the start and end parameters—those were added in 3.x to handle nested factors more explicitly:

fill_color=factor_cmap('x', palette=Viridis256, factors=years)

If you’re still getting errors, share the exact error message you’re seeing (e.g., import errors, runtime exceptions, or blank plots) and we can narrow it down further!

内容的提问来源于stack exchange,提问作者Matt P.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:36:09