Last modified: Jan 02, 2025 By Alexander Williams
Python Bokeh Legend: Customize Plot Element Labels
Legends are essential components of data visualizations that help viewers understand what different plot elements represent. In Bokeh, the legend
method provides powerful capabilities for adding and customizing legends.
Basic Legend Usage
To add a basic legend to your Bokeh plot, you can specify the legend label when creating plot elements. Here's a simple example with multiple lines:
from bokeh.plotting import figure, show
import numpy as np
# Create data
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
# Create plot
p = figure(title="Basic Legend Example")
# Add lines with legend labels
p.line(x, y1, line_color="blue", legend_label="Sine")
p.line(x, y2, line_color="red", legend_label="Cosine")
show(p)
Legend Positioning
You can control the legend's position using the legend_location
parameter. Bokeh offers several predefined positions like 'top_left', 'bottom_right', etc.
# Create plot with custom legend position
p = figure(title="Legend Position Example")
p.line(x, y1, line_color="blue", legend_label="Sine")
p.line(x, y2, line_color="red", legend_label="Cosine")
# Set legend position
p.legend.location = "top_right"
p.legend.click_policy="hide" # Makes legend items clickable
show(p)
Styling the Legend
Bokeh provides extensive options for customizing the legend's appearance. You can modify the background, border, text, and spacing properties.
p = figure(title="Styled Legend Example")
p.line(x, y1, line_color="blue", legend_label="Sine")
p.line(x, y2, line_color="red", legend_label="Cosine")
# Customize legend appearance
p.legend.background_fill_color = "lightgray"
p.legend.background_fill_alpha = 0.5
p.legend.border_line_color = "navy"
p.legend.border_line_width = 2
p.legend.label_text_font_size = "12px"
p.legend.spacing = 8
show(p)
For more complex visualizations, you might want to check out the Python Bokeh Scatter Plot Tutorial to see legends in action with different plot types.
Interactive Legends
Bokeh legends can be made interactive using the click_policy
property. This allows users to show/hide plot elements by clicking on legend items.
from bokeh.layouts import column
# Create two plots with interactive legends
p1 = figure(title="Hide Policy")
p1.line(x, y1, legend_label="Line 1")
p1.line(x, y2, legend_label="Line 2")
p1.legend.click_policy="hide"
p2 = figure(title="Mute Policy")
p2.line(x, y1, legend_label="Line 1")
p2.line(x, y2, legend_label="Line 2")
p2.legend.click_policy="mute"
show(column(p1, p2))
To enhance your interactive plots further, consider exploring the Python Bokeh HoverTool Guide for adding tooltips.
Legend Groups and Organizations
For complex plots with multiple elements, you can organize legends into groups using the LegendItem
class:
from bokeh.models import Legend
p = figure(title="Grouped Legend Example")
# Create multiple plot elements
l1 = p.line(x, y1, line_color="blue")
l2 = p.line(x, y2, line_color="red")
c1 = p.circle(x[::10], y1[::10], size=8, color="blue")
c2 = p.circle(x[::10], y2[::10], size=8, color="red")
# Create custom legend
legend = Legend(items=[
("Series 1", [l1, c1]),
("Series 2", [l2, c2]),
])
p.add_layout(legend)
show(p)
When working with multiple plots, you might find the Python Bokeh gridplot tutorial helpful for organizing your visualizations.
Conclusion
Bokeh's legend functionality provides a flexible way to add and customize legends in your plots. From basic usage to advanced styling and interactivity, you can create professional-looking visualizations.
Key takeaways include understanding legend positioning, styling options, interactive features, and organization methods. With these tools, you can create clear and informative data visualizations.