Skip to content

Instantly share code, notes, and snippets.

@joferkington
Created October 6, 2012 18:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save joferkington/3845684 to your computer and use it in GitHub Desktop.
Save joferkington/3845684 to your computer and use it in GitHub Desktop.
Arrows on the ends of spines for matplotlib
import matplotlib.pyplot as plt
def arrowed_spines(ax=None, arrow_length=20, labels=('', ''), arrowprops=None):
xlabel, ylabel = labels
if ax is None:
ax = plt.gca()
if arrowprops is None:
arrowprops = dict(arrowstyle='<|-', facecolor='black')
for i, spine in enumerate(['left', 'bottom']):
# Set up the annotation parameters
t = ax.spines[spine].get_transform()
xy, xycoords = [1, 0], ('axes fraction', t)
xytext, textcoords = [arrow_length, 0], ('offset points', t)
ha, va = 'left', 'bottom'
# If axis is reversed, draw the arrow the other way
top, bottom = ax.spines[spine].axis.get_view_interval()
if top < bottom:
xy[0] = 0
xytext[0] *= -1
ha, va = 'right', 'top'
if spine is 'bottom':
xarrow = ax.annotate(xlabel, xy, xycoords=xycoords, xytext=xytext,
textcoords=textcoords, ha=ha, va='center',
arrowprops=arrowprops)
else:
yarrow = ax.annotate(ylabel, xy[::-1], xycoords=xycoords[::-1],
xytext=xytext[::-1], textcoords=textcoords[::-1],
ha='center', va=va, arrowprops=arrowprops)
return xarrow, yarrow
@2t7
Copy link

2t7 commented Oct 24, 2014

Nice piece of code! A usage example would be nice, and maybe a comment on copyright / license

@lgblkb
Copy link

lgblkb commented May 25, 2016

Agree). Example would be nice.

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