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
Selected Reading
How to add bold annotated text in Matplotlib?
To add bold annotated text in Matplotlib, we can use LaTeX representation with \bf{} command. This is useful for emphasizing important labels or annotations in data visualizations.
Basic Bold Annotation Syntax
The key is using LaTeX formatting within the annotate() method ?
import matplotlib.pyplot as plt
plt.figure(figsize=(8, 6))
plt.plot([1, 2, 3], [1, 4, 2])
plt.annotate(r'$\bf{Bold\ Text}$', xy=(2, 4), xytext=(2.5, 3),
arrowprops=dict(arrowstyle='->', color='red'))
plt.title('Bold Annotation Example')
plt.show()
Complete Example with Scatter Plot
Here's how to add bold annotations to multiple data points ?
import numpy as np
import matplotlib.pyplot as plt
# Set figure properties
plt.rcParams["figure.figsize"] = [8, 6]
plt.rcParams["figure.autolayout"] = True
# Create data points
xpoints = np.linspace(1, 10, 5)
ypoints = np.random.rand(5)
labels = ["Point_%d" % i for i in range(1, 6)]
# Create scatter plot
plt.scatter(xpoints, ypoints, c=xpoints, s=100, cmap='viridis')
# Add bold annotations
for label, x, y in zip(labels, xpoints, ypoints):
plt.annotate(
r'$\bf{' + label + '}$',
xy=(x, y), xytext=(10, 15),
textcoords='offset points', ha='left', va='bottom',
arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0.1',
color='black', lw=1.5))
plt.xlabel('X Values')
plt.ylabel('Y Values')
plt.title('Scatter Plot with Bold Annotations')
plt.grid(True, alpha=0.3)
plt.show()
Different Bold Text Methods
You can also use alternative approaches for bold text ?
import matplotlib.pyplot as plt
fig, axes = plt.subplots(1, 3, figsize=(12, 4))
# Method 1: LaTeX bold
axes[0].text(0.5, 0.5, r'$\bf{LaTeX\ Bold}$', fontsize=14, ha='center')
axes[0].set_title('LaTeX Method')
# Method 2: FontWeight parameter
axes[1].text(0.5, 0.5, 'FontWeight Bold', fontsize=14, ha='center',
fontweight='bold')
axes[1].set_title('FontWeight Method')
# Method 3: HTML-style (limited support)
axes[2].text(0.5, 0.5, 'Standard Text', fontsize=14, ha='center')
axes[2].set_title('Standard Text')
plt.tight_layout()
plt.show()
Key Parameters
| Parameter | Description | Example |
|---|---|---|
r'$\bf{text}$' |
LaTeX bold formatting | r'$\bf{Bold\ Label}$' |
fontweight='bold' |
Font weight parameter | fontweight='bold' |
xytext |
Annotation position offset | xytext=(10, 20) |
arrowprops |
Arrow styling dictionary | dict(arrowstyle='->') |
Conclusion
Use LaTeX formatting r'$\bf{text}$' for bold annotations in Matplotlib. The annotate() method combined with LaTeX provides the most reliable way to create bold text with arrows pointing to specific data points.
Advertisements
