How to remove top and right axis in matplotlib?

Instead of the standard boxed style, I want to have only the left and bottom axes, that is:

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

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

+97
python matplotlib
May 29 '09 at 8:45 a.m.
source share
8 answers

This is the proposed 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

+104
Dec 08 '14 at 15:58
source share

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 have the same effect on the axis without losing support with label support.

(Matplotlib 1.0.1; solution inspired by this ).

+62
Nov 04 2018-11-11T00:
source share

[edit] matplotlib is currently (2013-10) in version 1.3.0, which includes this

This ability was just added, and for it you need a version of Subversion. You can see the sample code here .

I'm just updating to say that an example is better now. However, a version of Subversion is needed, but there have not yet been releases.

[edit] Matplotlib 0.99.0 RC1 has just been released and includes this feature.

+32
May 29 '09 at 9:21
source share

If you do not need ticks, etc. (for example, to build quality illustrations), you can also use this quick workaround:

Make the axis invisible (for example, using plt.gca().axison = False ), and then draw them manually using plt.arrow .

+9
May 29 '09 at 10:06
source share

(This is more of an additional comment, in addition to the comprehensive answers here.)




Please 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 checkmarks: ax.tick_params(top=False)

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

+7
Dec 27 '17 at 10:10
source share

The Seaborn library has a built-in despine () function.

Just add:

 import seaborn as sns 

Now create your schedule. And add at the end:

 sns.despine() 

If you look at some of the default values ​​of the function, it will remove the upper and right spine and save the lower and left parts of the spine:

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

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

+5
Jul 05 '18 at 7:48
source share

If you need to remove it from all your graphs, you can remove the spikes in the style settings (stylesheet or rcParams). For example:

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

If you want to remove all spikes:

 mpl.rcParams['axes.spines.left'] = False mpl.rcParams['axes.spines.right'] = False mpl.rcParams['axes.spines.top'] = False mpl.rcParams['axes.spines.bottom'] = False 
0
Aug 22 '19 at 21:59
source share

This is much more rudimentary, but can do the trick:

remove_border ()

-four
Jan 11 '16 at 5:44
source share



All Articles