In this tutorial, we will look at how to hide the axes in a matplotlib plot with the help of some examples.

You can use the following methods to hide one or more axes from a plot in matplotlib –

- If you want the hide both the axes (the x-axis and the y-axis) of a single matplotlib plot, use the
`matplolitb.pyplot.axis()`

function and pass ‘off’ as an argument. - To hide a specific axis, you can use the matplotlib axes object to get the respective axis using the
`.get_xaxis()`

or`.get_yaxis()`

function and set its visibility to`False`

using the`set_visible()`

function.

Let’s now look at some examples –

First, we will create a line plot that we will be using throughout this tutorial –

import matplotlib.pyplot as plt # x values - years x = [2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020] # y values - curde oil price per barrel in USD y = [94.88, 94.05, 97.98, 93.17, 48.66, 43.29, 50.80, 65.23, 56.99, 39.68] # plot x and y on a line plot plt.plot(x, y) # add axes labels plt.xlabel('Year') plt.ylabel('Crude oil price USD/barrel')

Output:

## Example 1 – Hide both the axes in a matplotlib plot

To hide both the axes – the x-axis and the y-axis, use the `matplotlib.pyplot.axis()`

function and pass ‘off’ as an argument (it’s an argument to the `option`

parameter). This turns off the axis ticks and labels.

# plot x and y on a line plot plt.plot(x, y) # add axes labels plt.xlabel('Year') plt.ylabel('Crude oil price USD/barrel') # turn off the axis plt.axis('off')

Output:

You can see that the axes are not present in the above-resulting plot.

**Data Science Programs By Skill Level**

**Introductory** ⭐

- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science

**Intermediate ⭐⭐⭐**

- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization

**Advanced ⭐⭐⭐⭐⭐**

- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science

**🔎 Find Data Science Programs 👨💻 111,889 already enrolled**

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

## Example 2 – Hide only the y-axis

In case you only want to hide a specific axis (for example, the y-axis), you can use the respective axes object’s `get_yaxis()`

function and set its visibility to `False`

.

The steps would be –

- Get the axes object of the respective plot.
- Get the axis which you want to hide using the axes object’s
`get_xaxis()`

of the`get_yaxis()`

function. - Set the visibility of the above axis to
`False`

using the`set_visible()`

function.

# plot x and y on a line plot plt.plot(x, y) # add axes labels plt.xlabel('Year') plt.ylabel('Crude oil price USD/barrel') # get the axes object ax = plt.gca() # hide the y-axis ax.get_yaxis().set_visible(False)

Output:

You can see that the y-axis ticks and labels have been removed.

## Example 3 – Hide only the x-axis

You can similarly hide only the x-axis –

# plot x and y on a line plot plt.plot(x, y) # add axes labels plt.xlabel('Year') plt.ylabel('Crude oil price USD/barrel') # get the axes object ax = plt.gca() # hide the x-axis ax.get_xaxis().set_visible(False)

Output:

The x-axis ticks and labels have been removed.

## Example 4 – Hide the axes in subplots

To hide a specific axis in a subplot, you can use the subplot’s axes object to access the respective axis and then set its visibility to `False`

like we did in the above examples.

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5)) # plot the first subplot ax1.plot(x, y) # add axis labels ax1.set_xlabel('Year') ax1.set_ylabel('Crude oil price USD/barrel') # plot the second subplot ax2.plot(x, y) # add axis labels ax2.set_xlabel('Year') ax2.set_ylabel('Crude oil price USD/barrel')

Output:

Here, we created two subplots. Both the subplots have the same data but that is not important. Let’s see how to hide the axis in each of the above subplots.

First, we will hide both axes from each of the subplots. For this, use the axes object’s `axis()`

function and pass `'off'`

as an argument.

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5)) # plot the first subplot ax1.plot(x, y) # add axis labels ax1.set_xlabel('Year') ax1.set_ylabel('Crude oil price USD/barrel') # hide both the axis ax1.axis('off') # plot the second subplot ax2.plot(x, y) # add axis labels ax2.set_xlabel('Year') ax2.set_ylabel('Crude oil price USD/barrel') # hide both the axis ax2.axis('off')

Output:

You can see that both axes were removed from each of the subplots.

To remove a specific axis from a subplot, use the respective axes object’s `get_xaxis()`

or `get_yaxis()`

method and set its visibility to `False`

.

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5)) # plot the first subplot ax1.plot(x, y) # add axis labels ax1.set_xlabel('Year') ax1.set_ylabel('Crude oil price USD/barrel') # hide the x-axis ax1.get_xaxis().set_visible(False) # plot the second subplot ax2.plot(x, y) # add axis labels ax2.set_xlabel('Year') ax2.set_ylabel('Crude oil price USD/barrel') # hide the y-axis ax2.get_yaxis().set_visible(False)

Output:

In the first subplot, we hide the x-axis and in the second subplot, we hide the y-axis. You can similarly remove both axes from a subplot using this method.

You might also be interested in –

- Matplotlib – Change the Number of Ticks in a Plot
- Remove Tick Labels from a Plot in Matplotlib
- Set Axis Range (axis limits) in Matplotlib Plots
- Reverse Axes of a Plot in Matplotlib
- Rotate Axis Labels in Matplotlib with Examples and Output

**Subscribe to our newsletter for more informative guides and tutorials. ****We do not spam and you can opt out any time.**