Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding axis text in matplotlib plots

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.

like image 216
Dave Avatar asked Feb 01 '10 11:02

Dave


People also ask

How do I hide Y axis in matplotlib?

How to hide axis in matplotlib figure? The matplotlib. pyplot. axis('off') command us used to hide the axis(both x-axis & y-axis) in the matplotlib figure.


2 Answers

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.

like image 123
Ofri Raviv Avatar answered Oct 04 '22 11:10

Ofri Raviv


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.

like image 29
Saullo G. P. Castro Avatar answered Oct 04 '22 11:10

Saullo G. P. Castro