How can I remove the top and right axis in matplotlib?

PythonMatplotlib

Python Problem Overview


Instead of the default "boxed" axis style I want to have only the left and bottom axis, i.e.:

+------+         |
|      |         |
|      |   --->  |
|      |         |
+------+         +-------

This should be easy, but I can't find the necessary options in the docs.

Python Solutions


Solution 1 - Python

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'].set_visible(False)
ax.spines['top'].set_visible(False)

# Only show ticks on the left and bottom spines
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')

plt.show()

enter image description here

Solution 2 - Python

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).

Solution 3 - Python

[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.

Solution 4 - Python

(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) or ax.spines['top'].set_visible(False)

  • To hide the ticks: ax.tick_params(top=False)

  • To hide the labels: ax.tick_params(labeltop=False)

Solution 5 - Python

If you need to remove it from all your plots, you can remove spines in style settings (style sheet or rcParams). E.g:

import matplotlib as mpl

mpl.rcParams['axes.spines.right'] = False
mpl.rcParams['axes.spines.top'] = False

If you want to remove all spines:

mpl.rcParams['axes.spines.left'] = False
mpl.rcParams['axes.spines.right'] = False
mpl.rcParams['axes.spines.top'] = False
mpl.rcParams['axes.spines.bottom'] = False

Solution 6 - Python

Library Seaborn has this built in with function .despine().

Just add:

import seaborn as sns

Now create your graph. And add at the end:

sns.despine()

If you look at some of the default parameter values of the function it removes the top and right spine and keeps the bottom and left spine:

sns.despine(top=True, right=True, left=False, bottom=False)

Check out further documentation here: https://seaborn.pydata.org/generated/seaborn.despine.html

Solution 7 - Python

If you don't need ticks and such (e.g. for plotting qualitative illustrations) you could also use this quick workaround:

Make the axis invisible (e.g. with plt.gca().axison = False) and then draw them manually with plt.arrow.

Solution 8 - Python

Another way of doing this in a non-global way is using the matplotlib.pyplot.rc_context context manager, as follows:

with plt.rc_context({
    "axes.spines.right": False,
    "axes.spines.top": False,
}):
    fig, ax = plt.subplots()
    ...

Solution 9 - Python

In case you want to apply this on a frequent basis, you can also create a style sheet as described here and use these settings:

axes.spines.left:   True
axes.spines.bottom: True
axes.spines.top:    False
axes.spines.right:  False

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMichael KuhnView Question on Stackoverflow
Solution 1 - PythondivenexView Answer on Stackoverflow
Solution 2 - PythontimdayView Answer on Stackoverflow
Solution 3 - PythonAutoplecticView Answer on Stackoverflow
Solution 4 - PythonEvgeni SergeevView Answer on Stackoverflow
Solution 5 - PythonhhhView Answer on Stackoverflow
Solution 6 - PythonSander van den OordView Answer on Stackoverflow
Solution 7 - PythonnikowView Answer on Stackoverflow
Solution 8 - PythonastrojuanluView Answer on Stackoverflow
Solution 9 - PythonDaveView Answer on Stackoverflow