Skip to content

Instantly share code, notes, and snippets.

@tacaswell
Created January 17, 2013 02:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tacaswell/4553135 to your computer and use it in GitHub Desktop.
Save tacaswell/4553135 to your computer and use it in GitHub Desktop.
Format nicely printed labels based on pi for matplotlib x-axis
def format_frac(fr):
'''Convert a/b to latex'''
sp = str(fr).split('/')
if len(sp) == 1:
return sp[0]
else:
return r'$\frac{%s}{%s}$' % tuple(sp)
frac_size = 4
step = fractions.Fraction(1, frac_size)
ax.set_xticks([np.pi * j * step for j in range(2 * frac_size + 1)])
ax.set_xticklabels([format_frac(j * step) + '$\pi$' for j in range(2 * frac_size + 1)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment