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

如何在pandas_bokeh饼图图例中显示DataFrame的status字段值?

Fixing Legend Display for pandas_bokeh Pie Chart

I've run into this exact issue before with pandas_bokeh's pie charts! The problem is that the library doesn't automatically map your status values to the legend labels by default, hence the generic __x__values text. Here's how to fix it:

Step 1: Update your pie chart code

Add the legend_label parameter and pass in the list of values from your status column. This explicitly tells pandas_bokeh what labels to use for the legend:

import pandas as pd
import pandas_bokeh

# Initialize your DataFrame
data = {
    "status": ["early", "on_time", "late", "Incomplete"],
    "count": [2, 2, 1, 9]
}
df2 = pd.DataFrame(data)

# Modified pie chart code with correct legend labels
df2.plot_bokeh.pie(
    x="status",
    y="count",
    legend="top_right",
    title="Task completion times",
    legend_label=df2["status"].tolist()  # This line fixes the legend
)

Why this works

By default, pandas_bokeh uses a placeholder for legend labels when you only specify x and y. Explicitly passing legend_label=df2["status"].tolist() maps each slice of the pie to its corresponding status value, so the legend will show "early", "on_time", "late", and "Incomplete" instead of the generic text.

Bonus: Improve hover tool readability

If you want to make the chart even more user-friendly, customize the hover tool to show both the status and count values:

df2.plot_bokeh.pie(
    x="status",
    y="count",
    legend="top_right",
    title="Task completion times",
    legend_label=df2["status"].tolist(),
    show_hovertool=True,
    hovertool_string="Status: @x<br>Count: @y"
)

This will display a clean tooltip with the status name and its corresponding count when you hover over any pie slice.

内容的提问来源于stack exchange,提问作者CINNI123486

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 12:02:42