484

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.

Chris
  • 42,054
  • 16
  • 135
  • 151
Dave
  • 5,506
  • 7
  • 22
  • 27
  • 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](https://stackoverflow.com/questions/24171064/matplotlib-remove-axis-label-offset-by-default). 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? – ImportanceOfBeingErnest Jan 05 '19 at 00:24

9 Answers9

603

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.

John Difool
  • 5,204
  • 4
  • 40
  • 70
Ofri Raviv
  • 23,474
  • 3
  • 53
  • 54
  • 2
    Hey Ofri - that's great, thanks, but now (just to be awkward) what happens if I want to add an xlabel()? Other than manually placing a text widget. – Dave Feb 01 '10 at 12:52
  • 7
    How can you extend this example to 3d? ax = plt.gca(projection='3d'), ax.axes.get_zaxis().set_visible(False) gives `'Axes3DSubplot' object has no attribute 'get_zaxis'` – user989762 Mar 26 '14 at 02:38
  • 17
    This answer does not allow grid lines to show. The answer by @saullo castro does though... – evan54 Mar 26 '14 at 20:48
  • 1
    The problem is that when the x_axis is set to invisible, the grid dsappears :( Edit: Just saw @Saullo Castro's answer which fixed the problem. – Shailen Sep 30 '14 at 15:38
  • 1
    @user989762 In your case this method won't work, not even `get_xaxis().set_visible(False)`. I think matplotlib in this case initializes some parameters as if the plot is 2D. `ax.set_zticks([])` will do what you want – kon psych Feb 09 '15 at 20:24
  • By the way I noticed that `frame1.axes` and `frame1` are the same object, but I don't know why one would need the first one. Is it for backwards compatibility? – kon psych Feb 09 '15 at 20:45
  • Thanks. This works great in 2D. This answer however does not work for `mplot3d`, but the answer by @saullo-castro does. – tommy.carstensen Sep 02 '15 at 19:49
  • That is exactly what I was looking for image display, thanks! – edison May 11 '17 at 15:45
273

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.

Saullo G. P. Castro
  • 53,388
  • 26
  • 170
  • 232
198

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([])
Ébe Isaac
  • 9,909
  • 16
  • 56
  • 91
108

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 splines:

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)
G M
  • 17,694
  • 10
  • 75
  • 78
  • 3
    `ax.set_xticklabels([])` and `ax.set_xticks([])` did not work for me, but `ax.axes.get_xaxis().set_visible(False)` did. – Homero Esmeraldo Dec 17 '19 at 17:17
  • `ax.axes.get_xaxis().set_visible(False)` also hid my `ax.set(xlabel='example')` but using `ax.set_xticklabels([])` and `ax.set_xticks([])` in tandem did not. – Alice Jan 18 '22 at 19:00
77

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='off', top='off', right='off', bottom='off', labelleft='off', labeltop='off', labelright='off', labelbottom='off')
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.

user
  • 4,960
  • 7
  • 46
  • 70
GingerLoaf
  • 961
  • 7
  • 6
  • 1
    When axes are shared, solutions based on set_ticklabels propagate on shared axes. Only this solution based on tick_params was working fine for me. – Christian O'Reilly Feb 22 '18 at 13:35
  • 4
    `plt.tick_params` expects a `bool` not a string. So `'off''` actually evaluates to `True`, so counterintuitively the command actually shows the ticks and labels. I found no information since when this is the behavior. – DerWeh Dec 28 '20 at 19:59
65

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())
Rafa Viotti
  • 9,250
  • 4
  • 39
  • 61
Nik Twerdochlib
  • 651
  • 5
  • 2
17

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".

Venkatachalam
  • 14,895
  • 9
  • 40
  • 70
  • 2
    I found this solution more simple than others, while I would add that color can be set to "None" in order not to depend on background color. – Armin Okić Aug 16 '20 at 15:38
  • 2
    I'd note this should be the string "None", and not thePython literal `None`. The latter throws an exception – ollien Jan 26 '21 at 02:02
14

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.

Steven C. Howell
  • 14,502
  • 13
  • 69
  • 84
2

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()