Removing the Top and Right axis (spines) in Matplotlib

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
4 min

banner

# Table of Contents

  1. Removing the Top and Right axis (spines) in Matplotlib
  2. Removing the Top and Right axis (spines) in Matplotlib using dot notation
  3. Removing the Top and Right axis (spines) in Matplotlib globally
  4. Hiding the spines with set_frame_on()
  5. Hiding the ticks or the labels
  6. Hide the top and right spines using Seaborn

# Removing the Top and Right axis (spines) in Matplotlib

To remove the top and right axis (spines) in Matplotlib:

  1. Use the matplotlib.spines property to get access to the top and right lines that connect the axis tick marks.
  2. Use the set_visible() method to remove the top and right axis (spines).
main.py
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 4*np.pi, 150) y = np.sin(x) ax = plt.subplot(111) ax.plot(x, y) # ๐Ÿ‘‡๏ธ remove the top and right axis ax.spines[['right', 'top']].set_visible(False) plt.show()

remove top and right axis in matplotlib

The code for this article is available on GitHub

We used the spines property to access the right and top spines.

The spines are the lines that connect the axis tick marks.

In order to access multiple spines at the same time, pass a list to the spines attribute.

main.py
# ๐Ÿ‘‡๏ธ remove the top and right axis ax.spines[['right', 'top']].set_visible(False)

You can also use this approach to remove the left and bottom spines.

main.py
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 4*np.pi, 150) y = np.sin(x) ax = plt.subplot(111) ax.plot(x, y) # ๐Ÿ‘‡๏ธ remove right, top, left and bottom spines ax.spines[['right', 'top', 'left', 'bottom']].set_visible(False) plt.show()

also removing left and bottom spines

The code for this article is available on GitHub

You can also use an open slice to remove all spines.

main.py
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 4*np.pi, 150) y = np.sin(x) ax = plt.subplot(111) ax.plot(x, y) # ๐Ÿ‘‡๏ธ remove the top and right axis ax.spines[:].set_visible(False) plt.show()

removing all spines with open slice

# Removing the Top and Right axis (spines) in Matplotlib using dot notation

You can also use dot notation to access the top and right spines and remove them by calling set_visible().

main.py
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 4*np.pi, 150) y = np.sin(x) ax = plt.subplot(111) ax.plot(x, y) # ๐Ÿ‘‡๏ธ remove the top spine ax.spines.top.set_visible(False) # ๐Ÿ‘‡๏ธ remove the right spine ax.spines.right.set_visible(False) plt.show()

remove top and right axis spines using dot notation

The code for this article is available on GitHub

Notice that w used dot notation when accessing the top and right spines and not bracket notation.

Calling the set_visible() method with a value of False removes the spines from the plot.

You can also achieve the same result by using bracket notation to access each spine individually.

main.py
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 4*np.pi, 150) y = np.sin(x) ax = plt.subplot(111) ax.plot(x, y) # ๐Ÿ‘‡๏ธ remove the top spine ax.spines['top'].set_visible(False) # ๐Ÿ‘‡๏ธ remove the right spine ax.spines['right'].set_visible(False) plt.show()

using bracket notation for each spine individually

The code for this article is available on GitHub

# Removing the Top and Right axis (spines) in Matplotlib globally

If you want to remove the top and right spines globally in Matplotlib, use your runtime rc settings.

main.py
import numpy as np import matplotlib.pyplot as plt plt.rcParams['axes.spines.right'] = False plt.rcParams['axes.spines.top'] = False x = np.linspace(0, 4*np.pi, 150) y = np.sin(x) ax = plt.subplot(111) ax.plot(x, y) plt.show()

use runtime rc params settings to remove top right axis globally

The code for this article is available on GitHub

The code sample sets rcParams (runtime configuration parameters) at runtime to remove the top and right spines.

Setting rcParams at runtime takes precedence over style sheets.

All runtime configuration settings are stored in a dictionary-like variable called matplotlib.rcParams.

The variable is global to the Matplotlib package, therefore the code sample removes the top and right spines globally.

The same approach can also be used to remove the left and bottom spines globally.

main.py
import numpy as np import matplotlib.pyplot as plt plt.rcParams['axes.spines.right'] = False plt.rcParams['axes.spines.top'] = False plt.rcParams['axes.spines.left'] = False plt.rcParams['axes.spines.bottom'] = False x = np.linspace(0, 4*np.pi, 150) y = np.sin(x) ax = plt.subplot(111) ax.plot(x, y) plt.show()

also removing left and bottom spines globally

# Hiding the spines with set_frame_on()

You can also use the axes.set_frame_on() method to hide the axes rectangle.

main.py
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 4*np.pi, 150) y = np.sin(x) ax = plt.subplot(111) ax.plot(x, y) ax.set_frame_on(False) plt.show()

hide axes rectangle

The code for this article is available on GitHub

# Hiding the ticks or the labels

If you need to hide the ticks or the labels, use the following 2 lines.

main.py
# hide ticks ax.tick_params(top=False) # hide labels ax.tick_params(labeltop=False)

# Hide the top and right spines using Seaborn

If you use the seaborn module, you can also use the seaborn.despine() method to hide the top and right spines.

First, make sure you have the seaborn module installed.

shell
pip install seaborn pip3 install seaborn

Now, import and use the module as follows.

main.py
import numpy as np import matplotlib.pyplot as plt import seaborn as sns x = np.linspace(0, 4*np.pi, 150) y = np.sin(x) ax = plt.subplot(111) ax.plot(x, y) sns.despine() plt.show()

remove top and right axis using seaborn

By default, the seaborn.despine() method removes the top and right spines.

You can also pass specific keyword arguments when calling despine().

Here is an equivalent example that explicitly sets the top, right, left and bottom keyword arguments when calling despine.

main.py
import numpy as np import matplotlib.pyplot as plt import seaborn as sns x = np.linspace(0, 4*np.pi, 150) y = np.sin(x) ax = plt.subplot(111) ax.plot(x, y) sns.despine(top=True, right=True, left=False, bottom=False) plt.show()

remove top and right axis using seaborn

The code for this article is available on GitHub

The example removes the top and right spines but does not remove the left and bottom spines because the arguments are set to False.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev