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 write annotation outside the drawing in data coords in Matplotlib?
In Matplotlib, you can place annotations outside the plotting area using the annotate() method with specific coordinate systems. This is useful for adding titles, labels, or explanatory text that shouldn't interfere with your data visualization.
Understanding Coordinate Systems
Matplotlib offers different coordinate systems for positioning annotations ?
- Data coordinates: Based on your actual data values
- Axes coordinates: Relative to the axes (0-1 range)
- Transform coordinates: Using axis transforms for positioning outside plot area
Basic Example with Transform Coordinates
Here's how to place an annotation outside the drawing area using axis transforms ?
import numpy as np
import matplotlib.pyplot as plt
# Set figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Create sample data
x = np.random.rand(100)
y = np.random.rand(100)
# Create plot
fig, ax = plt.subplots()
ax.scatter(x, y, c=y, marker="*", cmap="copper")
# Annotate outside the drawing area
ax.annotate('Scatter points (outside the drawing)',
xy=(0.30, 1.05),
xycoords=ax.get_xaxis_transform())
plt.show()
Different Positioning Methods
You can position annotations in various locations outside the plot ?
import numpy as np
import matplotlib.pyplot as plt
# Create data
x = np.linspace(0, 10, 50)
y = np.sin(x)
fig, ax = plt.subplots(figsize=(10, 6))
ax.plot(x, y, 'b-', linewidth=2)
# Top annotation using x-axis transform
ax.annotate('Top annotation',
xy=(5, 1.1),
xycoords=ax.get_xaxis_transform(),
ha='center')
# Right annotation using y-axis transform
ax.annotate('Right side',
xy=(1.02, 0.5),
xycoords=ax.get_yaxis_transform(),
rotation=90, va='center')
# Bottom annotation
ax.annotate('Bottom note',
xy=(8, -0.1),
xycoords=ax.get_xaxis_transform(),
ha='center')
plt.show()
Using Axes Coordinates
For more precise control, use axes coordinates where (0,0) is bottom-left and (1,1) is top-right ?
import matplotlib.pyplot as plt
import numpy as np
# Sample data
data = np.random.normal(0, 1, 1000)
fig, ax = plt.subplots(figsize=(8, 6))
ax.hist(data, bins=30, alpha=0.7, color='skyblue')
# Annotations using axes coordinates
ax.annotate('Outside top-left',
xy=(0.05, 1.05),
xycoords='axes fraction',
fontsize=12, color='red')
ax.annotate('Outside bottom-right',
xy=(1.02, -0.1),
xycoords='axes fraction',
fontsize=10, color='green')
plt.show()
Coordinate Systems Comparison
| Coordinate System | Usage | Range | Best For |
|---|---|---|---|
xycoords='data' |
Default data coordinates | Based on data values | Annotations tied to data points |
xycoords='axes fraction' |
Relative to axes | 0 to 1 | Consistent positioning regardless of data |
ax.get_xaxis_transform() |
Data x, axes y coordinates | Mixed | Annotations outside top/bottom |
ax.get_yaxis_transform() |
Axes x, data y coordinates | Mixed | Annotations outside left/right |
Conclusion
Use ax.get_xaxis_transform() for annotations outside the top/bottom of plots, and ax.get_yaxis_transform() for left/right positioning. Axes coordinates provide the most flexible control for positioning annotations outside the drawing area.
