Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove the x-axis ticks while keeping the grids (matplotlib) [duplicate]

I want to remove the ticks on the x-axis but keep the vertical girds. When I do the following I lose both x-axis ticks as well as the grid.

import matplotlib.pyplot as plt fig = plt.figure()  figr = fig.add_subplot(211) ... figr.axes.get_xaxis().set_visible(False) figr.xaxsis.grid(True) 

How can I retain the grid while makeing x-axis ticks invisible?

like image 841
DurgaDatta Avatar asked Dec 06 '13 05:12

DurgaDatta


People also ask

What is the use of Xticks () and Yticks () in plotting?

You can use the xticks() and yticks() functions and pass in an array denoting the actual ticks. On the X-axis, this array starts on 0 and ends at the length of the x array. On the Y-axis, it starts at 0 and ends at the max value of y . You can hard code the variables in as well.

What are the Matplotlib methods we can use to adjust your X-axis tick marks?

Method 1 : xticks() and yticks() We can also set labels of the ticks of the axes using these functions, but, here we will focus only on changing the interval of ticks of axes. To know more about these functions click on xticks() and yticks().


1 Answers

By removing the ticks, do you mean remove the tick labels or the ticks themselves? This will remove the labels:

import matplotlib.pyplot as plt import numpy as np  x = np.linspace(0, 2*np.pi, 100)  fig = plt.figure() ax = fig.add_subplot(111)  ax.plot(x, np.sin(x))  ax.grid(True) ax.set_xticklabels([])   plt.show() 

If you really want to get rid of the little tick lines, you can add this:

for tic in ax.xaxis.get_major_ticks():     tic.tick1On = tic.tick2On = False 

You could turn the tick labels off here too without resorting to the ax.set_xticklabels([]) "hack" by setting tic.label1On = tic.label2On = False:

import matplotlib.pyplot as plt import numpy as np  x = np.linspace(0, 2*np.pi, 100)  fig = plt.figure() ax = fig.add_subplot(111)  ax.plot(x, np.sin(x))  ax.grid(True) for tick in ax.xaxis.get_major_ticks():     tick.tick1line.set_visible(False)     tick.tick2line.set_visible(False)     tick.label1.set_visible(False)     tick.label2.set_visible(False)  plt.show() 
like image 105
mgilson Avatar answered Sep 22 '22 23:09

mgilson