565

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)]
plt.plot(rx,ry,'ko')

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

plt.show()

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 4x4 subplots in a figure, if that is relevant.

1
  • I have a little doubt if any of the answers here actually answer the question. To me it looks like the question asks how to get rid of the offset. Yet all the answers show various ways how to get rid of all the ticklabels. If the question has been superseeded by the answers, maybe one should edit the question to ask for what the answers provide solutions for? Commented Jan 5, 2019 at 0:24

13 Answers 13

683

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

frame1.axes.get_xaxis().set_visible(False)
frame1.axes.get_yaxis().set_visible(False)

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

frame1.axes.get_xaxis().set_ticks([])
frame1.axes.get_yaxis().set_ticks([])

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

0
308

If you want to hide just the axis text keeping the grid lines:

frame1 = plt.gca()
frame1.axes.xaxis.set_ticklabels([])
frame1.axes.yaxis.set_ticklabels([])

Doing set_visible(False) or set_ticks([]) will also hide the grid lines.

0
227

If you are like me and don't always retrieve the axes, ax, when plotting the figure, then a simple solution would be to do

plt.xticks([])
plt.yticks([])
2
  • 6
    I just do plt.axis('off') for a one-stop shop.
    – eric
    Commented Nov 1, 2022 at 13:23
  • 3
    This also removes gridlines (if any). Commented Feb 6, 2023 at 14:32
171

I've colour coded this figure to ease the process.

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)

enter image description here

You can have full control over the figure using these commands, to complete the answer I've add also the control over the spines:

ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

# X AXIS -BORDER
ax.spines['bottom'].set_visible(False)
# BLUE
ax.set_xticklabels([])
# RED
ax.set_xticks([])
# RED AND BLUE TOGETHER
ax.axes.get_xaxis().set_visible(False)

# Y AXIS -BORDER
ax.spines['left'].set_visible(False)
# YELLOW
ax.set_yticklabels([])
# GREEN
ax.set_yticks([])
# YELLOW AND GREEN TOGHETHER
ax.axes.get_yaxis().set_visible(False)
0
106
+50

I was not actually able to render an image without borders or axis data based on any of the code snippets here (even the one accepted at the answer). After digging through some API documentation, I landed on this code to render my image

plt.axis('off')
plt.tick_params(axis='both', left=False, top=False, right=False, bottom=False, labelleft=False, labeltop=False, labelright=False, labelbottom=False)
plt.savefig('foo.png', dpi=100, bbox_inches='tight', pad_inches=0.0)

I used the tick_params call to basically shut down any extra information that might be rendered and I have a perfect graph in my output file.

0
67

Somewhat of an old thread but, this seems to be a faster method using the latest version of matplotlib:

set the major formatter for the x-axis

ax.xaxis.set_major_formatter(plt.NullFormatter())
1
  • This method also keeps ticks and grids intact
    – Roux
    Commented Jan 2, 2023 at 12:13
20

One trick could be setting the color of tick labels as white to hide it!

plt.xticks(color='w')
plt.yticks(color='w')

or to be more generalized (@Armin Okić), you can set it as "None".

0
18

When using the object oriented API, the Axes object has two useful methods for removing the axis text, set_xticklabels() and set_xticks().

Say you create a plot using

fig, ax = plt.subplots(1)
ax.plot(x, y)

If you simply want to remove the tick labels, you could use

ax.set_xticklabels([])

or to remove the ticks completely, you could use

ax.set_xticks([])

These methods are useful for specifying exactly where you want the ticks and how you want them labeled. Passing an empty list results in no ticks, or no labels, respectively.

5

You could simply set xlabel to None, straight in your axis. Below an working example using seaborn

from matplotlib import pyplot as plt
import seaborn as sns

tips = sns.load_dataset("tips")

ax = sns.boxplot(x="day", y="total_bill", data=tips)
ax.set(xlabel=None)

plt.show()
2

Just do this in case you have subplots

fig, axs = plt.subplots(1, 2, figsize=(16, 8))

ax[0].set_yticklabels([]) # x-axis
ax[0].set_xticklabels([]) # y-axis
1
  • Axis comments are reversed, but otherwise it works.
    – Alex Poca
    Commented Mar 20, 2023 at 13:33
2

How to let the tick labels stay but the axis label go away:

axs.xaxis.label.set_visible(False)

1

Try with the following line of code:

plt.axis("off")

Should solve the issue for you.

1

To keep the ticks and grid lines intact but hide the tick labels, use:

ax.xaxis.set_tick_params(labelcolor='none')
# Similar for the y axis.

This also has the advantage that the mouse-over utility of the interactive backend of Jupyter Notebook is not affected; most of the other solutions tend to cause the mouse-over utility to defunct, i.e. not showing the plot coordinates.

Not the answer you're looking for? Browse other questions tagged or ask your own question.