How to hide the axis in Matplotlib Figure?

To hide the axis, you have to call matplotlib.pyplot.axis() function and pass 'off' or False to it. It is important to note that this function will hide both the X-axis and Y-axis.

import matplotlib.pyplot as plt

x = [1,2,3,4,5,6]
y = [1,2,3,4,5,6]
plt.axis('off')
plt.plot(x, y)
plt.show()

Output

Matplotlib hide axis

How to hide X-axis in Matplotlib?

If you want to hide X-axis, then follow the below procedure:

  1. Get the current axes object by calling the plt.gca() function.
  2. After that, call xaxis.set_visible(False) on the axes object obtained in the previous step to hide the x-axis.
import matplotlib.pyplot as plt

x = [1,2,3,4,5,6]
y = [1,2,3,4,5,6]
ax = plt.gca()
ax.xaxis.set_visible(False)
plt.plot(x, y)
plt.show()

Output

Matplotlib hide X-axis

How to hide Y-axis in Matplotlib?

If you want to hide Y-axis, then follow the below procedure:

  1. Get the current axes object by calling the plt.gca() function.
  2. After that, call yaxis.set_visible(False) on the axes object obtained in the previous step to hide the y-axis.
import matplotlib.pyplot as plt

x = [1,2,3,4,5,6]
y = [1,2,3,4,5,6]
ax = plt.gca()
ax.yaxis.set_visible(False)
plt.plot(x, y)
plt.show()

Output

Matplotlib hide Y-axis

Note: axes.get_xaxis() and axes.get_yaaxis() are discouraged by Matplotlib documentation so it is recommended that you avoid using these two functions.