Skip to content

Instantly share code, notes, and snippets.

@vsbuffalo
Created April 29, 2020 17:39
Show Gist options
  • Save vsbuffalo/f058c1358f6ed5f3f4e43c8b65584c6e to your computer and use it in GitHub Desktop.
Save vsbuffalo/f058c1358f6ed5f3f4e43c8b65584c6e to your computer and use it in GitHub Desktop.
def fix_spines(ax, connect=True, x=True):
"""
Beautifies spines by stopping them at the last tick mark. If connect=False,
also stops them at the first tick mark. For y-axis only, set x=False.
"""
ylim = ax.get_ylim()
xlim = ax.get_xlim()
yticks = ax.get_yticks()
xticks = ax.get_xticks()
ylast = yticks[yticks <= ylim[1]][-1]
xlast = xticks[xticks <= xlim[1]][-1]
if connect:
yfirst = ylim[0]
xfirst = xlim[0]
else:
yfirst = yticks[yticks >= ylim[0]][0]
xfirst = xticks[xticks >= xlim[0]][0]
ylimits = yfirst, ylast
xlimits = xfirst, xlast
ax.spines['left'].set_bounds(*ylimits)
if x:
ax.spines['bottom'].set_bounds(*xlimits)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment