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 put xtick labels in a box matplotlib?
To put xtick labels in a box in matplotlib, we use the set_bbox() method on tick label objects. This creates a visible box around each x-axis label with customizable styling.
Steps
Create a new figure or activate an existing figure
Get the current axis of the figure
Position the spines and ticks as needed
Iterate through the x-tick labels using
get_xticklabels()Apply
set_bbox()method with desired box propertiesDisplay the figure using
show()method
Basic Example
Here's how to add boxes around x-tick labels ?
import matplotlib.pyplot as plt
import numpy as np
# Create sample data
x = np.linspace(0, 10, 6)
y = np.sin(x)
plt.figure(figsize=(8, 4))
plt.plot(x, y, 'b-', marker='o')
# Get current axis
ax = plt.gca()
# Add boxes around x-tick labels
for label in ax.get_xticklabels():
label.set_fontsize(10)
label.set_bbox(dict(facecolor='lightblue', edgecolor='black', alpha=0.8))
plt.title('X-tick Labels in Boxes')
plt.grid(True, alpha=0.3)
plt.show()
Customizing Box Properties
You can customize the box appearance with different parameters ?
import matplotlib.pyplot as plt
import numpy as np
# Create sample data
categories = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
values = [23, 45, 56, 78, 32]
plt.figure(figsize=(10, 6))
plt.bar(categories, values, color='skyblue')
ax = plt.gca()
# Customize boxes around x-tick labels
for label in ax.get_xticklabels():
label.set_fontsize(12)
label.set_bbox(dict(
facecolor='yellow', # Background color
edgecolor='red', # Border color
linewidth=2, # Border thickness
alpha=0.7, # Transparency
boxstyle='round,pad=0.3' # Rounded corners
))
plt.title('Monthly Sales with Boxed Labels')
plt.ylabel('Sales')
plt.show()
Box Style Options
| Box Style | Description | Example Parameter |
|---|---|---|
square |
Square box | boxstyle='square,pad=0.3' |
round |
Rounded corners | boxstyle='round,pad=0.3' |
sawtooth |
Sawtooth edges | boxstyle='sawtooth,pad=0.3' |
Complete Example with Data Visualization
import matplotlib.pyplot as plt
import numpy as np
# Create sample dataset
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
sales = [120, 150, 180, 200, 170, 190]
plt.figure(figsize=(10, 6))
plt.plot(months, sales, 'go-', linewidth=2, markersize=8)
ax = plt.gca()
# Position spines at zero
ax.spines['bottom'].set_position(('data', 100))
ax.spines['left'].set_position(('data', 0))
# Hide top and right spines
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
# Add boxes to x-tick labels
for label in ax.get_xticklabels():
label.set_fontsize(11)
label.set_bbox(dict(
facecolor='lightgreen',
edgecolor='darkgreen',
alpha=0.8,
boxstyle='round,pad=0.2'
))
plt.title('Sales Trend with Boxed Month Labels', fontsize=14)
plt.ylabel('Sales (in thousands)', fontsize=12)
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
Conclusion
Use set_bbox() on x-tick labels to create visible boxes around axis labels. Customize the appearance using parameters like facecolor, edgecolor, and boxstyle for better visual presentation.
