Open In App

Adding X and Y Axis Labels to Bokeh Figures

Last Updated : 10 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

Bokeh is a powerful Python library for creating interactive visualizations in web browsers. When creating plots or figures in Bokeh, adding clear and descriptive labels for the X and Y axes is crucial for improving the readability and interpretability of the graph. Customizing these axis labels can also help you make your plots more visually appealing and aligned with your presentation or reporting style.

This article will guide you through setting and customizing axis labels in Bokeh, including changing their font size, style, and color. We will also provide example code to demonstrate how to apply these customizations.

Setting Axis Labels in Bokeh

Adding labels to the X and Y axes of a Bokeh figure is quite simple. The xaxis.axis_label and yaxis.axis_label properties allow you to define labels for both axes. These labels help users understand what the data represents, especially in cases where numeric or categorical axes could be ambiguous without proper context.

Basic Syntax for Setting Axis Labels:

Python
from bokeh.plotting import figure, show
from bokeh.io import output_notebook

output_notebook()

# Create a new figure
p = figure(width=400, height=400, title="Simple Line Plot")

# Add a line plot to the figure
p.line([1, 2, 3, 4], [1, 4, 9, 16])

# Set X and Y axis labels
p.xaxis.axis_label = "X Axis Label"
p.yaxis.axis_label = "Y Axis Label"

# Show the figure
show(p)

Output:

Screenshot-2024-09-10-151213
Axis Labels in Bokeh

Explanation:

  • p.xaxis.axis_label is used to set the label for the X-axis.
  • p.yaxis.axis_label is used to set the label for the Y-axis.

In this example, we create a simple line plot, and both axis labels are applied to describe the data.

Customizing Axis Labels With Bokeh

Bokeh offers a wide range of customization options for axis labels, including font size, style, color, and alignment. These customizations are done using the axis_label_text_font_size, axis_label_text_font_style, and axis_label_text_color properties.

Common Customizations:

  • Font Size: Adjust the size of the text using axis_label_text_font_size. You can specify the size in px, pt, em, etc.
  • Font Style: Change the style (normal, italic, bold) using axis_label_text_font_style.
  • Color: Customize the text color using axis_label_text_color. It accepts any valid CSS color format (e.g., "red", "#FF5733").

Code Example for Customizing Axis Labels:

Python
# Customize X and Y axis labels
p.xaxis.axis_label_text_font_size = "14pt"
p.xaxis.axis_label_text_font_style = "bold"
p.xaxis.axis_label_text_color = "blue"

p.yaxis.axis_label_text_font_size = "12pt"
p.yaxis.axis_label_text_font_style = "italic"
p.yaxis.axis_label_text_color = "green"

# Show the figure with customized labels
show(p)

Output:

Screenshot-2024-09-10-151232
Customizing Axis Labels With Bokeh

Explanation:

  • Font Size: The X-axis label is set to 14pt, and the Y-axis label is set to 12pt.
  • Font Style: The X-axis label is bold, while the Y-axis label is italic.
  • Text Color: The X-axis label is blue, and the Y-axis label is green.

These customizations can help make your graph look more professional and better aligned with the design or branding you are using in your reports or presentations.

Example: Customized X and Y Axis Labels

Below is a complete example that demonstrates setting and customizing axis labels in a Bokeh figure. We’ll apply different font sizes, styles, and colors to the X and Y axes, and show the resulting plot.

Python
from bokeh.plotting import figure, show
from bokeh.io import output_notebook

output_notebook()

# Create a new figure
p = figure(width=400, height=400, title="Customized Axis Labels")

# Add a line plot to the figure
p.line([1, 2, 3, 4], [1, 4, 9, 16])

# Set X and Y axis labels
p.xaxis.axis_label = "Time (s)"
p.yaxis.axis_label = "Distance (m)"

# Customize X axis label
p.xaxis.axis_label_text_font_size = "14pt"
p.xaxis.axis_label_text_font_style = "bold"
p.xaxis.axis_label_text_color = "darkblue"

# Customize Y axis label
p.yaxis.axis_label_text_font_size = "12pt"
p.yaxis.axis_label_text_font_style = "italic"
p.yaxis.axis_label_text_color = "darkgreen"

# Show the figure with customized labels
show(p)

Output:

Screenshot-2024-09-10-151252
Customized X and Y Axis Labels

Explanation:

  • X-Axis Label: Labeled as "Time (s)", the font size is set to 14pt, bold style, and dark blue color.
  • Y-Axis Label: Labeled as "Distance (m)", the font size is set to 12pt, italic style, and dark green color.
  • The title of the figure is set to "Customized Axis Labels".

This example demonstrates a simple yet effective way to customize your axis labels, ensuring they provide clarity and fit well within your visualizations.

Conclusion

Customizing X and Y axis labels in Bokeh is a simple but powerful way to enhance the readability and aesthetics of your visualizations. With just a few lines of code, you can set axis labels and adjust their font size, style, and color to match your design needs. By customizing these elements, you can make your plots more professional and informative, especially when presenting them to stakeholders or in reports.

Using Bokeh's extensive customization options, you can control nearly every aspect of your figure’s appearance, making it a great tool for producing interactive, visually appealing visualizations in Python.


Similar Reads