Skip to content

Instantly share code, notes, and snippets.

@hjhilden
Last active September 16, 2025 12:30
Show Gist options
  • Select an option

  • Save hjhilden/334ea1450f115ca7f88ea6bd743b450d to your computer and use it in GitHub Desktop.

Select an option

Save hjhilden/334ea1450f115ca7f88ea6bd743b450d to your computer and use it in GitHub Desktop.
Add bracket function for matplotlib
# Bracket annotation function
def add_bracket(ax, x0, x1, y, text='', xycoords='data', bracket_height=12, pointer_height=6, arrowprops=None, color='blue', linewidth=1, rad=10, **kwargs):
"""
Adds a bracket annotation to the given axes.
Returns the last annotation patch.
Parameters:
ax (matplotlib.axes._axes.Axes): The axes to annotate.
x0 (float): The starting x-coordinate of the bracket.
x1 (float): The ending x-coordinate of the bracket.
y (float): The y-coordinate of the bracket.
text (str, optional): Annotation text. Default is an empty string.
xycoords (str, optional): coordinate system for positioning.
bracket_height (float, optional): The height of the bracket offset. Default is 12.
pointer_height (float, optional): Height of the text pointer. Default is 6.
arrowprops (dict, optional): Properties for the arrow. Default is None.
color (str, optional): Color of the bracket and pointer. Default is 'blue'.
linewidth (float, optional): Line width of the bracket and pointer. Default is 1.
rad (float, optional): Radius of the bracket corners. Default is 10.
kwargs: additional arguments passed to annotate (font size etc.).
Returns:
matplotlib.text.Annotation: The last annotation patch added to the axes.
"""
if arrowprops is None:
arrowprops = dict(
color=color,
arrowstyle="-",
connectionstyle=f"angle,angleA=0,angleB=90,rad={rad}",
relpos=(1, 0.5),
shrinkA=0,
shrinkB=0,
linewidth=linewidth
)
# bracket arms
ann1 = ax.annotate('', xy=(x0, y), xycoords=xycoords, textcoords=('data', 'offset points'), xytext=((x1 + x0) / 2, bracket_height), arrowprops=arrowprops)
ann2 = ax.annotate('', xy=(x1, y), xycoords=xycoords, textcoords=('data', 'offset points'), xytext=((x1 + x0) / 2, bracket_height), arrowprops=arrowprops)
# mid pointer and text (optional), position relative to ann1
ann_end = ax.annotate(text, xy=(1,1), xycoords=ann1, textcoords='offset points', xytext=(0, pointer_height),
bbox=dict(boxstyle="square,pad=0.1",
fc='#fff0', ec='white',
lw=0 ),
arrowprops=dict(
arrowstyle="-",
shrinkA=0,
shrinkB=0,
relpos=(0, 0.5),
color=color,
linewidth=linewidth),
**kwargs
)
return ann_end
# Example usage:
add_bracket(ax, x0=1.56, x1=7.84, y=1, text='Hello brackets', bracket_height=22, pointer_height=12, fontsize=12)
add_bracket(ax, x0=4.71, x1=7.84, y=1, text='Im smol', bracket_height=12, pointer_height=12, color='red', rad=15)
@hjhilden
Copy link
Author

hjhilden commented Sep 16, 2025

Draws brackets like this by leveraging matplotlib's built-in annotate function.
https://matplotlib.org/stable/users/explain/text/annotations.html#annotations
two sine wave plots and example of annotation brackets use

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment