How can I remove the top and right axis in matplotlib?
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Island
--
Chapters
00:00 Question
00:29 Accepted answer (Score 254)
00:56 Answer 2 (Score 72)
01:23 Answer 3 (Score 33)
02:04 Answer 4 (Score 24)
02:31 Thank you
--
Full question
https://stackoverflow.com/questions/9250...
Accepted answer links:
[HERE]: http://matplotlib.org/examples/ticks_and...
Answer 2 links:
[this]: http://www.shocksolution.com/2011/08/rem.../
Answer 3 links:
[here]: http://matplotlib.org/examples/pylab_exa...
[example]: https://github.com/ericliang/matplotlib/...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #matplotlib
#avk47
ACCEPTED ANSWER
Score 291
This is the suggested Matplotlib 3 solution from the official website HERE:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
ax = plt.subplot(111)
ax.plot(x, y)
# Hide the right and top spines
ax.spines[['right', 'top']].set_visible(False)
plt.show()

ANSWER 2
Score 74
Alternatively, this
def simpleaxis(ax):
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
seems to achieve the same effect on an axis without losing rotated label support.
(Matplotlib 1.0.1; solution inspired by this).
ANSWER 3
Score 33
[edit] matplotlib in now (2013-10) on version 1.3.0 which includes this
That ability was actually just added, and you need the Subversion version for it. You can see the example code here.
I am just updating to say that there's a better example online now. Still need the Subversion version though, there hasn't been a release with this yet.
[edit] Matplotlib 0.99.0 RC1 was just released, and includes this capability.
ANSWER 4
Score 26
(This is more of an extension comment, in addition to the comprehensive answers here.)
Note that we can hide each of these three elements independently of each other:
To hide the border (aka "spine"):
ax.set_frame_on(False)orax.spines['top'].set_visible(False)To hide the ticks:
ax.tick_params(top=False)To hide the labels:
ax.tick_params(labeltop=False)