Customizing Spines & Axis Position
Spines are the four border lines around a plot, and customizing them lets you hide clutter, recolor edges, or move the axes to cross at the origin for a clean, professional look.
Learn Customizing Spines & Axis Position in our free Matplotlib course — a beginner-friendly interactive lesson with worked examples, a practice exercise and…
Part of the free Matplotlib course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this lesson you'll hide the top and right borders, recolor and thicken spines, shorten them with bounds, and move the left and bottom spines to zero for a centered, math-style axis.
Every Axes has four spines stored in ax.spines : 'top' , 'bottom' , 'left' , and 'right' . The single most popular tweak in all of Matplotlib is hiding the top and right ones — it removes the box and instantly modernizes the chart.
What you'll see: a crimson sine wave with only an L-shaped frame — the left and bottom spines remain, but the top and right borders are gone, so the chart feels open and uncluttered.
A spine is a full artist, so you can restyle it. Use set_color() and set_linewidth() to change how it looks, and set_bounds(low, high) to draw it only across part of the axis — handy for emphasizing the range your data actually covers.
To get the classic math-textbook cross through the origin, move the left and bottom spines to the data value 0 with set_position('zero') , then hide the top and right spines. Finally, tell the ticks to follow by calling ax.xaxis.set_ticks_position('bottom') and ax.yaxis.set_ticks_position('left') .
What you'll see: the purple cubic curve passing through a cross formed by the x and y axes meeting at the center of the plot, with tick numbers running along both arms of the cross.
Replace each ___ to hide the top and right spines of this scatter plot.
Spine names are lowercase. ✅ Use 'top' , 'bottom' , 'left' , 'right' — never capitalized.
Moving a spine does not move the ticks. ✅ Call ax.xaxis.set_ticks_position('bottom') and ax.yaxis.set_ticks_position('left') after repositioning.
Spines live on an Axes , not on plt . ✅ Get an axes first with fig, ax = plt.subplots() , then use ax.spines[...] .
Plot a cosine curve, hide the top and right spines, recolor the left and bottom spines gray, and thicken them to 1.5.
Lesson complete — you control the frame!
You hid the top and right spines, recolored and thickened the rest, shortened a spine with bounds, and centered the axes at the origin for a math-textbook look.
🚀 Up next: Advanced Legends — place legends outside the axes, build custom handles, and add more than one legend at a time.
Practice quiz
What are the four spines of an Axes?
- north, south, east, west
- top, bottom, left, right
- x, y, z, w
- up, down, in, out
Answer: top, bottom, left, right. Spines are keyed 'top', 'bottom', 'left', and 'right' in ax.spines.
How do you hide the top spine?
- top
Answer: top. set_visible(False) on the spine hides it.
Where do spines live?
- On plt
- On the Figure
- On the colorbar
- On the Axes (ax.spines)
Answer: On the Axes (ax.spines). Spines belong to an Axes; access them via ax.spines[...].
Which call moves the left spine to the data value 0?
- left
Answer: left. set_position('zero') moves the spine to data value 0.
Which method shortens a spine to part of the axis?
- set_length()
- set_range()
- set_bounds(low, high)
- set_limit()
Answer: set_bounds(low, high). set_bounds(low, high) draws the spine only across that range.
Which call recolors a spine to navy?
- left
set_color('navy') changes the spine's color.
Why might ticks stay put after you move a spine?
- Moving a spine does not move ticks
- Ticks are broken
- You must call plt.show() twice
- set_position deletes ticks
Answer: Moving a spine does not move ticks. Call ax.xaxis.set_ticks_position(...) to make ticks follow the moved spine.
What does ax.spines['left'].set_linewidth(2.5) do?
- Hides the spine
- Moves the spine
- Recolors the spine
- Thickens the spine
Answer: Thickens the spine. set_linewidth changes the spine's thickness in points.
Why does ax.spines['Top'] raise KeyError?
- Spines are uppercase
- Spine names are lowercase
- Top is not a spine
- It needs an index
Answer: Spine names are lowercase. Spine names are lowercase: use 'top', not 'Top'.
Which is the single most common spine tweak?
- Coloring all spines red
- Moving every spine to zero
- Hiding the top and right spines
- Thickening the bottom spine
Answer: Hiding the top and right spines. Hiding the top and right spines gives charts a clean, modern look.