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 text at the corner of an equal aspect figure in Python/Matplotlib?
To put text at the corner of an equal aspect figure in Matplotlib, you can use the annotate() method with xycoords='axes fraction' parameter. This allows you to position text using relative coordinates where (0,0) is the bottom-left corner and (1,1) is the top-right corner.
Steps to Add Corner Text
Set the figure size and adjust the padding between and around the subplots.
Create a figure and a set of subplots using
subplots()method.Create x data points using NumPy.
Plot data on different axes using
plot()method.Use
annotate()method withxycoords='axes fraction'to position text at corners.Display the figure using
show()method.
Example
Here's how to add text at different corners of subplot axes ?
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
fig, axes = plt.subplots(2)
x = np.linspace(-2, 2, 10)
axes[0].plot(x)
axes[1].plot(x, 2*x)
for ax in axes:
ax.annotate('Top Right', xy=(1, 1), xycoords='axes fraction',
fontsize=10, horizontalalignment='right',
verticalalignment='top')
ax.annotate('Bottom Left', xy=(0, 0), xycoords='axes fraction',
fontsize=10, horizontalalignment='left',
verticalalignment='bottom')
plt.show()
Corner Position Coordinates
When using xycoords='axes fraction', the coordinates work as follows ?
| Corner | Coordinates (x, y) | horizontalalignment | verticalalignment |
|---|---|---|---|
| Bottom Left | (0, 0) | left | bottom |
| Bottom Right | (1, 0) | right | bottom |
| Top Left | (0, 1) | left | top |
| Top Right | (1, 1) | right | top |
Advanced Example with Multiple Corners
You can add text to all four corners of a single plot ?
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(figsize=(6, 6))
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
ax.plot(x, y, 'b-', linewidth=2)
ax.set_aspect('equal')
# Add text to all four corners
corners = [
((0, 0), 'Bottom Left', 'left', 'bottom'),
((1, 0), 'Bottom Right', 'right', 'bottom'),
((0, 1), 'Top Left', 'left', 'top'),
((1, 1), 'Top Right', 'right', 'top')
]
for (x_pos, y_pos), text, h_align, v_align in corners:
ax.annotate(text, xy=(x_pos, y_pos), xycoords='axes fraction',
fontsize=12, horizontalalignment=h_align,
verticalalignment=v_align, bbox=dict(boxstyle='round,pad=0.3',
facecolor='yellow', alpha=0.7))
plt.title('Text at All Four Corners')
plt.show()
Key Parameters
xy− Coordinates where to place the textxycoords='axes fraction'− Use relative coordinates (0-1 range)horizontalalignment− Text alignment ('left', 'center', 'right')verticalalignment− Vertical alignment ('top', 'center', 'bottom')fontsize− Size of the textbbox− Optional background box styling
Conclusion
Use annotate() with xycoords='axes fraction' to position text at corners using relative coordinates. Combine with proper alignment parameters for precise corner placement in equal aspect figures.
