Bokeh is a powerful and flexible visualization library in Python that allows you to create interactive plots and dashboards. When creating visualizations, it's often necessary to customize the appearance of your plots to make them more visually appealing and easier to understand. One such customization is adjusting the title font size of your Bokeh figure to ensure that it stands out or fits within the overall design of your plot.
In this article, we'll explore how to adjust the title font size for a Bokeh figure, along with some additional tips for customizing your plot titles.
Table of Content
Understanding Bokeh's Title Customization
Every Bokeh figure can have a title that describes the plot's content. By default, Bokeh assigns a standard font size to the title, but this may not always be suitable for your needs. Depending on the context, you might want to increase or decrease the title font size to improve readability or to match your plot's style.
Bokeh allows users to customize various aspects of plot titles, such as font size, color, alignment, and more. These customizations help in making the visualizations more appealing and informative.
Adjusting Title Font Size for a Bokeh Plot
To add a title to a Bokeh plot, you can use the title parameter when creating a figure. Here's a simple example:
from bokeh.plotting import figure, output_file, show, output_notebook
# Create a figure with a title
p = figure(title="Basic Title", width=300, height=300)
p.circle([1, 2], [3, 4])
output_file("title.html")
show(p)
Output:
For more, refer to the link : Title chart
Adjusting Title Font Size
To adjust the font size of the title, you can use the text_font_size attribute of the title property. This attribute allows you to specify the font size in various units such as pixels (px), points (pt), or em units. An example of how to change the title font size:
from bokeh.plotting import figure, output_file, show
# Create a figure
p = figure(title="My Custom Title", width=300, height=300)
p.circle([1, 2], [3, 4])
# Customize the title font size
p.title.text_font_size = '20pt'
output_file("custom_title.html")
show(p)
Output:
In this example, the title font size is set to 20 points. You can adjust the size by changing the value of text_font_size.
Advanced Title Customization
Bokeh provides several other properties to customize the title further:
- Font Family: You can change the font family using the
text_fontproperty. For example,p.title.text_font = 'times'. - Font Color: Adjust the title color using
text_color. For example,p.title.text_color = '#434244'. - Alignment: Use the
alignproperty to align the title. Options include"left","center", and"right". - Background Color: Set the background color of the title with
background_fill_color.
from bokeh.plotting import figure, output_file, show
# Create a figure
p = figure(width=300, height=300)
p.circle([1, 2], [3, 4])
# Customize the title
p.title.text = "Customized Title"
p.title.text_font_size = '25px'
p.title.text_color = "orange"
p.title.text_font = "arial"
p.title.align = "right"
p.title.background_fill_color = "#aaaaee"
output_file("full_custom_title.html")
show(p)
Output:
In this example, the title is customized with a specific font size, color, font family, alignment, and background color.
Handling Multiple Titles with Bokeh
Bokeh allows you to add more than one title to a plot using the add_layout() method. This can be useful when you need to provide additional context or information.
from bokeh.models import Title
from bokeh.plotting import figure, output_file, show
# Create a figure
p = figure(title="Main Title", width=300, height=300)
p.circle([1, 2], [3, 4])
# Add an additional title
p.add_layout(Title(text="Secondary Title", align="center"), "below")
output_file("multiple_titles.html")
show(p)
Output:

Tips for Choosing Font Sizes
- Context Matters: The appropriate font size depends on the overall size of your figure and the medium through which it will be viewed. For instance, a larger font size might be necessary for presentations or dashboards viewed from a distance, while smaller sizes could be sufficient for reports or web pages.
- Consistency: If you're creating multiple plots for a single report or dashboard, maintain consistency in font sizes across titles to create a cohesive visual style.
- Readability: Ensure that the title remains readable regardless of the font size. Avoid setting the font size too small, especially if the title contains important information.
Conclusion
Customizing the title font size in a Bokeh figure is a simple yet effective way to enhance the visual appeal of your plots. By adjusting the font size and style, you can ensure that your titles are clear, attractive, and consistent with the overall design of your visualizations. Whether you're creating a single plot or a complex dashboard, these adjustments will help you communicate your data more effectively.