Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to change ticks label sizes using Python’s Bokeh
Bokeh is a powerful Python data visualization library that creates interactive plots and dashboards for web browsers. When working with Bokeh plots, you may need to customize the tick label sizes to improve readability and visual appeal. This article demonstrates how to change tick label sizes using various Bokeh formatting options.
Understanding Tick Labels in Bokeh
Tick labels are the text labels that appear along the axes of a plot, indicating the values at specific tick marks. In Bokeh, you can customize these labels using the axis properties and formatting options.
Installing Bokeh
First, install the Bokeh library using pip ?
pip install bokeh
Method 1: Using CSS-style Font Size
The most straightforward way to change tick label sizes is by modifying the major_label_text_font_size property ?
from bokeh.plotting import figure, show
from bokeh.io import output_file
# Create sample data
x = [1, 2, 3, 4, 5]
y = [2, 5, 3, 6, 1]
# Create figure
p = figure(width=600, height=400, title="Customized Tick Label Sizes")
# Add scatter plot
p.scatter(x, y, size=10, color='blue')
# Customize tick label font sizes
p.xaxis.major_label_text_font_size = "16pt"
p.yaxis.major_label_text_font_size = "14pt"
# Display the plot
output_file("tick_labels.html")
show(p)
Method 2: Using Axis Properties
You can also customize other text properties along with font size ?
from bokeh.plotting import figure, show
from bokeh.io import output_file
import numpy as np
# Generate data
x = np.linspace(0, 10, 50)
y = np.sin(x)
# Create figure
p = figure(width=700, height=400, title="Sine Wave with Custom Tick Labels")
# Add line plot
p.line(x, y, line_width=2, color='red')
# Customize x-axis tick labels
p.xaxis.major_label_text_font_size = "12pt"
p.xaxis.major_label_text_color = "navy"
p.xaxis.major_label_text_font_style = "bold"
# Customize y-axis tick labels
p.yaxis.major_label_text_font_size = "10pt"
p.yaxis.major_label_text_color = "green"
output_file("sine_wave.html")
show(p)
Method 3: Combining with Tick Formatters
You can change tick label sizes while using custom formatters ?
from bokeh.plotting import figure, show
from bokeh.models import NumeralTickFormatter
from bokeh.io import output_file
import numpy as np
# Create data
x = np.arange(1000, 6000, 500)
y = np.random.randint(100, 1000, len(x))
# Create figure
p = figure(width=650, height=450, title="Sales Data with Formatted Ticks")
# Add bar chart
p.vbar(x=x, top=y, width=300, color='orange', alpha=0.7)
# Apply formatters and customize font sizes
p.xaxis.formatter = NumeralTickFormatter(format="$0,0")
p.yaxis.formatter = NumeralTickFormatter(format="0,0")
# Set tick label font sizes
p.xaxis.major_label_text_font_size = "15pt"
p.yaxis.major_label_text_font_size = "12pt"
# Additional styling
p.xaxis.major_label_orientation = 45
p.title.text_font_size = "16pt"
output_file("sales_chart.html")
show(p)
Available Font Size Options
| Property | Description | Example Values |
|---|---|---|
major_label_text_font_size |
Font size for major tick labels | "10pt", "12px", "14pt" |
major_label_text_font_style |
Font style | "normal", "bold", "italic" |
major_label_text_color |
Text color | "black", "#FF0000", "blue" |
Key Points
Use
major_label_text_font_sizeto change tick label sizesFont sizes can be specified in points ("12pt") or pixels ("12px")
You can customize x-axis and y-axis labels independently
Combine font size changes with formatters for better presentation
Conclusion
Customizing tick label sizes in Bokeh is straightforward using the major_label_text_font_size property. This allows you to improve plot readability and create more professional-looking visualizations that fit your specific design requirements.
