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 draw a line outside of an axis in Matplotlib?
When creating matplotlib plots, you may need to draw lines or arrows outside the main plotting area for annotations or visual emphasis. The annotate() method provides a flexible way to add lines outside the axis boundaries.
Understanding Coordinate Systems
To draw outside the axis, we use xycoords='axes fraction' where coordinates are expressed as fractions of the axis dimensions. Values outside 0-1 range extend beyond the axis boundaries.
Basic Example
Here's how to draw a horizontal line below the axis ?
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
fig = plt.figure(1)
fig.clf()
ax = fig.add_subplot(1, 1, 1)
# Draw a line outside the axis (below)
ax.annotate('', xy=(0, -0.1), xycoords='axes fraction',
xytext=(1, -0.1),
arrowprops=dict(arrowstyle="<->", color='b'))
plt.show()
Multiple Lines Example
You can add multiple lines in different positions ?
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(figsize=(8, 6))
# Sample data
x = np.linspace(0, 10, 100)
y = np.sin(x)
ax.plot(x, y, 'r-', label='sin(x)')
# Line below axis
ax.annotate('', xy=(0, -0.15), xycoords='axes fraction',
xytext=(1, -0.15),
arrowprops=dict(arrowstyle="-", color='blue', lw=2))
# Line above axis
ax.annotate('', xy=(0, 1.05), xycoords='axes fraction',
xytext=(1, 1.05),
arrowprops=dict(arrowstyle="-", color='green', lw=2))
# Vertical line on the left
ax.annotate('', xy=(-0.1, 0), xycoords='axes fraction',
xytext=(-0.1, 1),
arrowprops=dict(arrowstyle="-", color='orange', lw=2))
ax.set_title('Lines Outside Axis Boundaries')
ax.legend()
plt.tight_layout()
plt.show()
Arrow Styles and Customization
Different arrow styles can create various line effects ?
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(10, 6))
# Plot some data
ax.plot([1, 2, 3, 4], [1, 4, 2, 3], 'ro-')
# Different arrow styles
styles = ["-", "<-", "->", "<->", "<|-|>"]
colors = ['red', 'blue', 'green', 'purple', 'orange']
for i, (style, color) in enumerate(zip(styles, colors)):
y_pos = -0.1 - i * 0.05
ax.annotate('', xy=(0, y_pos), xycoords='axes fraction',
xytext=(1, y_pos),
arrowprops=dict(arrowstyle=style, color=color, lw=2))
# Add label
ax.text(1.02, y_pos, f'arrowstyle="{style}"',
transform=ax.transAxes, va='center', fontsize=10)
ax.set_title('Different Arrow Styles Outside Axis')
plt.subplots_adjust(right=0.7, bottom=0.4)
plt.show()
Key Parameters
| Parameter | Description | Example Values |
|---|---|---|
xy |
End point coordinates | (1, -0.1) |
xytext |
Start point coordinates | (0, -0.1) |
xycoords |
Coordinate system | 'axes fraction' |
arrowstyle |
Line/arrow appearance | '-', '<->', '->' |
Conclusion
Use annotate() with xycoords='axes fraction' to draw lines outside matplotlib axes. Values below 0 or above 1 extend beyond the axis boundaries, allowing flexible positioning of annotations and visual elements.
