Last modified: Jan 02, 2025 By Alexander Williams

Python Bokeh Axis Configuration Guide

In data visualization, properly configured axes are crucial for clear and effective plots. Bokeh's xaxis() and yaxis() methods provide powerful tools to customize axis properties and labels.

Basic Axis Configuration

Let's start with a simple example of how to configure basic axis properties in Bokeh. We'll create a plot with customized axis labels and formatting.


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

# Create a simple plot
p = figure(width=600, height=400)
p.circle([1, 2, 3, 4, 5], [2, 5, 8, 2, 7])

# Configure axis labels
p.xaxis.axis_label = 'X Axis Label'
p.yaxis.axis_label = 'Y Axis Label'

# Customize label properties
p.xaxis.axis_label_text_font_size = '14pt'
p.yaxis.axis_label_text_font_style = 'bold'

show(p)

Axis Orientation and Position

Bokeh allows you to control the position and orientation of axes. You can place axes on different sides of the plot and customize their appearance.


# Create a figure with multiple axes
p = figure(width=600, height=400)
p.line([1, 2, 3, 4, 5], [2, 5, 8, 2, 7])

# Add an additional y-axis on the right
p.extra_y_ranges = {"second": Range1d(start=0, end=100)}
p.add_layout(LinearAxis(y_range_name="second"), 'right')

# Customize axis positions
p.xaxis.major_label_orientation = 0.7  # Rotate x-axis labels
p.yaxis.major_label_orientation = "vertical"

show(p)

Formatting Tick Labels

One of the most common tasks is formatting axis tick labels. Bokeh provides several ways to customize how numbers and dates are displayed on axes.


from bokeh.models import NumeralTickFormatter

p = figure(width=600, height=400)
p.line([1, 2, 3, 4, 5], [2000, 5000, 8000, 2000, 7000])

# Format y-axis labels as currency
p.yaxis.formatter = NumeralTickFormatter(format="$0,0")

# Format x-axis labels with custom precision
p.xaxis.formatter = NumeralTickFormatter(format="0.00")

show(p)

Grid Lines and Ticks

Customizing grid lines and tick marks can greatly improve the readability of your plots. Learn how to modify these elements with Bokeh's axis properties.


p = figure(width=600, height=400)
p.line([1, 2, 3, 4, 5], [2, 5, 8, 2, 7])

# Customize grid lines
p.xgrid.grid_line_color = 'gray'
p.xgrid.grid_line_dash = [6, 4]
p.ygrid.grid_line_alpha = 0.3

# Modify tick properties
p.axis.major_tick_line_color = 'red'
p.axis.minor_tick_line_color = None

show(p)

For more complex visualizations, you might want to add interactive features using the Bokeh HoverTool or enhance your plot with custom annotations using add_layout().

Hiding and Showing Axis Elements

Sometimes you may want to hide certain axis elements or create minimal plots. Here's how to control the visibility of different axis components.


p = figure(width=600, height=400)
p.line([1, 2, 3, 4, 5], [2, 5, 8, 2, 7])

# Hide various axis elements
p.xaxis.visible = False  # Hide entire x-axis
p.yaxis.major_label_text_color = None  # Hide y-axis labels
p.grid.visible = False  # Hide all grid lines

show(p)

Common Axis Styling Properties

Here's a list of commonly used properties for axis customization:

  • axis_label: Set axis label text
  • axis_label_text_font_size: Control label font size
  • major_label_orientation: Rotate tick labels
  • formatter: Configure number/date formatting
  • visible: Show/hide entire axis

Conclusion

Mastering axis configuration in Bokeh is essential for creating professional-looking visualizations. Combine these techniques with proper titles to create clear, informative plots.