Fix Python – Hiding axis text in matplotlib plots


Asked By – Dave

I’m trying to plot a figure without tickmarks or numbers on either of the axes (I use axes in the traditional sense, not the matplotlib nomenclature!). An issue I have come across is where matplotlib adjusts the x(y)ticklabels by subtracting a value N, then adds N at the end of the axis.

This may be vague, but the following simplified example highlights the issue, with ‘6.18’ being the offending value of N:

import matplotlib.pyplot as plt
import random
prefix = 6.18

rx = [prefix+(0.001*random.random()) for i in arange(100)]
ry = [prefix+(0.001*random.random()) for i in arange(100)]

frame1 = plt.gca()
for xlabel_i in frame1.axes.get_xticklabels():
for xlabel_i in frame1.axes.get_yticklabels():
for tick in frame1.axes.get_xticklines():
for tick in frame1.axes.get_yticklines():

The three things I would like to know are:

  1. How to turn off this behaviour in the first place (although in most cases it is useful, it is not always!) I have looked through matplotlib.axis.XAxis and cannot find anything appropriate

  2. How can I make N disappear (i.e. X.set_visible(False))

  3. Is there a better way to do the above anyway? My final plot would be 4×4 subplots in a figure, if that is relevant.

Now we will see solution for issue: Hiding axis text in matplotlib plots


Instead of hiding each element, you can hide the whole axis:


Or, you can set the ticks to an empty list:


In this second option, you can still use plt.xlabel() and plt.ylabel() to add labels to the axes.

This question is answered By – Ofri Raviv

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0