如何在pandas_bokeh饼图图例中显示DataFrame的status字段值?
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

