Last modified: Jan 02, 2025 By Alexander Williams
Python Bokeh Theme: Style Plots with Custom Themes
Bokeh's theme()
function provides a powerful way to apply consistent styling across your visualizations. Whether you're using built-in themes or creating custom ones, theming helps maintain visual consistency.
Understanding Bokeh Themes
Themes in Bokeh control various visual aspects of plots, including colors, fonts, grid lines, and more. They help maintain consistency across multiple plots and can significantly enhance visualization aesthetics.
The integration with axis configuration and plot titles makes themes a crucial component in creating professional-looking visualizations.
Using Built-in Themes
Bokeh comes with several built-in themes. Here's how to apply them:
from bokeh.io import output_file, show
from bokeh.plotting import figure
from bokeh.themes import built_in_themes
from bokeh.io import curdoc
# Create a basic plot
p = figure(width=600, height=400)
p.line([1, 2, 3, 4, 5], [2, 5, 8, 2, 7])
# Apply built-in dark theme
curdoc().theme = built_in_themes['dark_minimal']
# Show the plot
show(p)
Creating Custom Themes
You can create custom themes by defining a theme dictionary or JSON file. Here's an example of a custom theme:
from bokeh.themes import Theme
custom_theme = {
'attrs': {
'figure': {
'background_fill_color': '#f5f5f5',
'border_fill_color': '#ffffff',
'outline_line_color': '#444444'
},
'axis': {
'axis_line_color': '#444444',
'axis_label_text_font': 'Arial',
'axis_label_text_font_style': 'bold'
},
'grid': {
'grid_line_color': '#dddddd'
}
}
}
# Apply custom theme
curdoc().theme = Theme(json=custom_theme)
# Create and show plot
p = figure(width=600, height=400)
p.line([1, 2, 3, 4, 5], [2, 5, 8, 2, 7])
show(p)
Theme Properties
Common theme properties that you can customize include:
- Background colors
- Border styles
- Text properties
- Grid line styles
- Default colors for plots
Advanced Theme Configuration
You can create more complex themes by combining multiple elements. Here's an advanced example:
advanced_theme = {
'attrs': {
'figure': {
'background_fill_color': '#f0f0f0',
'border_fill_color': '#ffffff',
'outline_line_color': '#333333',
'outline_line_width': 2
},
'axis': {
'axis_line_color': '#333333',
'axis_label_text_font': 'Helvetica',
'axis_label_text_font_size': '14pt',
'major_label_text_font': 'Helvetica',
'major_label_text_font_size': '12pt'
},
'grid': {
'grid_line_color': '#cccccc',
'grid_line_alpha': 0.3
},
'title': {
'text_font': 'Helvetica',
'text_font_size': '16pt',
'text_font_style': 'bold'
}
}
}
# Apply advanced theme
curdoc().theme = Theme(json=advanced_theme)
# Create example plot
p = figure(width=600, height=400, title="Advanced Theme Example")
p.line([1, 2, 3, 4, 5], [2, 5, 8, 2, 7])
p.circle([1, 2, 3, 4, 5], [2, 5, 8, 2, 7], size=8)
show(p)
Theme Integration with Layouts
Themes work seamlessly with multiple plot layouts, ensuring consistent styling across all plots in your visualization.
Best Practices for Theme Usage
When working with themes, consider these important practices:
- Keep themes consistent across related visualizations
- Test themes with different plot types
- Consider accessibility when choosing colors
- Document custom themes for team collaboration
Conclusion
Bokeh's theming capabilities provide a flexible way to create visually appealing and consistent plots. Whether using built-in themes or creating custom ones, proper theming enhances visualization quality.
Remember to balance aesthetic appeal with readability and accessibility when designing custom themes. For complex visualizations, consider combining themes with other Bokeh features for optimal results.