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 set a Matplotlib rectangle edge to outside of specified width?
To set a Matplotlib rectangle edge to outside of a specified width, you need to use AnnotationBbox with AuxTransformBox. This technique allows you to create a rectangle with an outer border that extends beyond the rectangle's actual boundaries.
Basic Rectangle with Outer Edge
Here's how to create a rectangle with an edge extending outside its specified width ?
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from matplotlib.offsetbox import AnnotationBbox, AuxTransformBox
# Set figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Create figure and subplot
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1, 6, 8, 6])
# Rectangle parameters
line_width = 7
xy, w, h = (1, 3), 2, 2
# Create the main rectangle
r = Rectangle(xy, w, h, fc='green', ec='orange', lw=3)
# Create offset transform box
offsetbox = AuxTransformBox(ax.transData)
offsetbox.add_artist(r)
# Create annotation box with outer edge
ab = AnnotationBbox(offsetbox, (xy[0] + w/2, xy[1] + h/2),
boxcoords="data", pad=0.52, fontsize=line_width,
bboxprops=dict(facecolor="none", edgecolor='red',
lw=line_width))
ax.add_artist(ab)
plt.show()
How It Works
The technique involves several key components:
- Rectangle: The inner rectangle with specified dimensions
- AuxTransformBox: Container that holds the rectangle
- AnnotationBbox: Wrapper that adds the outer edge through bboxprops
- bboxprops: Dictionary defining the outer edge properties
Customizing the Outer Edge
You can customize the appearance of the outer edge by modifying the bboxprops ?
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from matplotlib.offsetbox import AnnotationBbox, AuxTransformBox
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4))
# Example 1: Thick blue outer edge
xy, w, h = (1, 1), 2, 1.5
r1 = Rectangle(xy, w, h, fc='lightblue', ec='navy', lw=2)
offsetbox1 = AuxTransformBox(ax1.transData)
offsetbox1.add_artist(r1)
ab1 = AnnotationBbox(offsetbox1, (xy[0] + w/2, xy[1] + h/2),
boxcoords="data", pad=0.3,
bboxprops=dict(facecolor="none", edgecolor='blue',
lw=8, linestyle='--'))
ax1.add_artist(ab1)
ax1.set_title('Dashed Blue Outer Edge')
ax1.set_xlim(0, 4)
ax1.set_ylim(0, 3)
# Example 2: Thin red outer edge with padding
xy2, w2, h2 = (1, 1), 2, 1.5
r2 = Rectangle(xy2, w2, h2, fc='lightyellow', ec='orange', lw=2)
offsetbox2 = AuxTransformBox(ax2.transData)
offsetbox2.add_artist(r2)
ab2 = AnnotationBbox(offsetbox2, (xy2[0] + w2/2, xy2[1] + h2/2),
boxcoords="data", pad=0.8,
bboxprops=dict(facecolor="lightgray", edgecolor='red',
lw=4, alpha=0.7))
ax2.add_artist(ab2)
ax2.set_title('Red Edge with Gray Fill')
ax2.set_xlim(0, 4)
ax2.set_ylim(0, 3)
plt.tight_layout()
plt.show()
Key Parameters
| Parameter | Purpose | Example Values |
|---|---|---|
pad |
Distance between rectangle and outer edge | 0.3, 0.5, 0.8 |
lw (linewidth) |
Thickness of outer edge | 2, 5, 8 |
edgecolor |
Color of outer edge | 'red', 'blue', '#FF5733' |
facecolor |
Fill color of outer border area | 'none', 'lightgray', 'white' |
Conclusion
Use AnnotationBbox with AuxTransformBox to create rectangles with outer edges. The pad parameter controls the distance, while bboxprops customizes the outer edge appearance.
Advertisements
