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 text inside a plot in Matplotlib?
Adding text inside a plot in Matplotlib allows you to annotate charts with labels, equations, or explanatory notes. You can use the text() function to place text at specific coordinates with customizable styling.
Basic Text Placement
The plt.text() function takes x and y coordinates along with the text string ?
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.figure(figsize=(8, 5))
plt.plot(x, y, 'b-', linewidth=2)
plt.text(5, 0.5, 'Sine Wave', fontsize=14, color='red')
plt.title('Basic Text Example')
plt.grid(True, alpha=0.3)
plt.show()
Text with Mathematical Expressions
Use LaTeX syntax within dollar signs for mathematical formulas ?
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-5, 5, 100)
y = x ** 3
plt.figure(figsize=(8, 6))
plt.plot(x, y, 'g-', linewidth=2)
plt.text(0, 50, r'$y = x^3$', fontsize=20,
bbox=dict(facecolor='yellow', alpha=0.7, pad=10))
plt.text(-3, -80, r'$\frac{dy}{dx} = 3x^2$', fontsize=16,
bbox=dict(facecolor='lightblue', alpha=0.7))
plt.title('Mathematical Expressions in Text')
plt.grid(True, alpha=0.3)
plt.show()
Text Styling and Positioning
Control text appearance with font properties, colors, and background boxes ?
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2*np.pi, 100)
y1 = np.sin(x)
y2 = np.cos(x)
plt.figure(figsize=(10, 6))
plt.plot(x, y1, 'b-', label='sin(x)')
plt.plot(x, y2, 'r-', label='cos(x)')
# Different text styling examples
plt.text(1, 0.8, 'Maximum', fontsize=12, weight='bold',
ha='center', va='bottom', color='blue')
plt.text(4.5, -0.8, 'Minimum', fontsize=12, style='italic',
ha='center', va='top', color='red')
plt.text(3, 0.5, 'Intersection\nPoint', fontsize=10,
bbox=dict(boxstyle='round,pad=0.5', facecolor='wheat', alpha=0.8),
ha='center', va='center')
plt.title('Styled Text Examples')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
Text Positioning Options
| Parameter | Options | Description |
|---|---|---|
ha |
left, center, right | Horizontal alignment |
va |
bottom, center, top | Vertical alignment |
weight |
normal, bold | Font weight |
style |
normal, italic | Font style |
Multiple Text Elements
Add multiple text annotations to highlight different parts of your plot ?
import matplotlib.pyplot as plt
import numpy as np
# Generate data
x = np.linspace(0, 10, 100)
y = np.exp(-x/3) * np.sin(x)
plt.figure(figsize=(10, 6))
plt.plot(x, y, 'purple', linewidth=2)
# Add multiple text annotations
texts = [
(1, 0.6, 'Peak 1', 'orange'),
(4.2, -0.3, 'Valley', 'red'),
(7.3, 0.15, 'Peak 2', 'blue'),
(8.5, 0.1, 'Decay', 'green')
]
for x_pos, y_pos, text, color in texts:
plt.text(x_pos, y_pos, text, fontsize=11, color=color,
bbox=dict(facecolor='white', alpha=0.8, edgecolor=color))
plt.title('Damped Oscillation with Annotations')
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.grid(True, alpha=0.3)
plt.show()
Conclusion
Use plt.text() to add annotations at specific coordinates. Style text with font properties, colors, and background boxes. Use LaTeX syntax for mathematical expressions within dollar signs.
Advertisements
