Skip to Content

Matplolib – Hide Axis in a Plot (Code with Examples)

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

hide axis in a matplotlib plot

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

  1. 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.
  2. 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:

the resulting line chart with crude oil price data

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:

both axes removed from the line chart

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

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 –

  1. Get the axes object of the respective plot.
  2. Get the axis which you want to hide using the axes object’s get_xaxis() of the get_yaxis() function.
  3. 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:

yaxis hidden in the line chart

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:

xaxis hidden in the line chart

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:

the resulting plot with subplots

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:

both axes removed from each subplot

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:

only specific axis removed from subplots

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 –


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


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.