35

I'm trying to plot data in matplotlib. I would like to hide the upper and right parts of the box. Does anyone know how to do this?

Thanks for your help

1
  • @Joe: Thanks I didn't know about the concept of 'spines' in Matplotlib.
    – mac389
    Mar 17, 2012 at 16:02

2 Answers 2

62

Just set the spines (and/or ticks) to be invisible.

E.g.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)

plt.show()

enter image description here

If you want to hide the ticks on the top and left as well, just do:

ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

enter image description here

16

Consider also this option:

import matplotlib.pyplot as plt
# Create your plot with your code
# Then extract the spines and make them invisible
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
plt.show() # Show your plot

or just

import matplotlib.pyplot as plt
# Create your plot with your code
# Then extract the spines and make them invisible
plt.gca().spines['right'].set_color('none')
plt.gca().spines['top'].set_color('none')
plt.show() # Show your plot

Hope it helps somebody

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.