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 add footnote under the X-axis using Matplotlib?
To add a footnote under the X-axis using Matplotlib, we can use the figtext() method to place text at specific coordinates on the figure. This is useful for adding citations, data sources, or explanatory notes.
Using figtext() Method
The figtext() method allows you to place text anywhere on the figure using normalized coordinates (0 to 1). Position (0, 0) is the bottom-left corner, and (1, 1) is the top-right corner.
Example
Here's how to add a footnote with a styled box under the X-axis ?
import numpy as np
import matplotlib.pyplot as plt
# Set figure size and layout
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Create data points
x = np.linspace(-2, 2, 100)
y = np.exp(x)
# Create the plot
plt.plot(x, y)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Exponential Function')
# Add footnote under the X-axis
plt.figtext(0.5, 0.01, "footnote: $y=e^{x}$",
ha="center", fontsize=12,
bbox={"facecolor": "lightblue", "alpha": 0.8, "pad": 5})
plt.show()
Using text() Method
Alternatively, you can use the text() method with data coordinates to place footnotes relative to your plot ?
import numpy as np
import matplotlib.pyplot as plt
# Create data
x = np.linspace(0, 10, 50)
y = np.sin(x)
# Create plot
fig, ax = plt.subplots(figsize=(8, 4))
ax.plot(x, y)
ax.set_xlabel('Time (seconds)')
ax.set_ylabel('Amplitude')
ax.set_title('Sine Wave')
# Add footnote using text() method
ax.text(5, -1.5, "Data source: Generated using numpy.sin()",
ha='center', fontsize=10, style='italic',
bbox=dict(boxstyle="round,pad=0.3", facecolor="yellow", alpha=0.7))
plt.tight_layout()
plt.show()
Key Parameters
| Parameter | Description | Example Values |
|---|---|---|
ha |
Horizontal alignment | 'left', 'center', 'right' |
va |
Vertical alignment | 'bottom', 'center', 'top' |
fontsize |
Text size | 8, 10, 12, 'small', 'large' |
bbox |
Box styling | Dictionary with facecolor, alpha, pad |
Multiple Footnotes
You can add multiple footnotes at different positions ?
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 4*np.pi, 100)
y1 = np.sin(x)
y2 = np.cos(x)
plt.figure(figsize=(10, 5))
plt.plot(x, y1, label='sin(x)')
plt.plot(x, y2, label='cos(x)')
plt.legend()
plt.title('Trigonometric Functions')
# Add multiple footnotes
plt.figtext(0.15, 0.02, "Note 1: sin(x) oscillates between -1 and 1",
fontsize=9, style='italic')
plt.figtext(0.65, 0.02, "Note 2: cos(x) leads sin(x) by ?/2",
fontsize=9, style='italic')
plt.tight_layout()
plt.show()
Conclusion
Use figtext() with normalized coordinates for consistent footnote positioning across different figure sizes. Use text() when you need footnotes positioned relative to your data coordinates.
