Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python hide ticks but show tick labels

I can remove the ticks with

ax.set_xticks([])  ax.set_yticks([])  

but this removes the labels as well. Any way I can plot the tick labels but not the ticks and the spine

like image 651
user308827 Avatar asked May 01 '15 13:05

user308827


People also ask

How do you hide a tick in Python?

By using the method xticks() and yticks() you can disable the ticks and tick labels from both the x-axis and y-axis. In the above example, we use plt. xticks([]) method to invisible both the ticks and labels on the x-axis and set the ticks empty.

How do you put a tick on a label in Python?

To fix the position of ticks, use set_xticks() function. To set string labels at x-axis tick labels, use set_xticklabels() function.

How do you hide Xlabel?

To hide or remove X-axis labels, use set(xlabel=None). To display the figure, use show() method.


2 Answers

You can set the tick length to 0 using tick_params (http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.tick_params):

fig = plt.figure() ax = fig.add_subplot(111) ax.plot([1],[1]) ax.tick_params(axis=u'both', which=u'both',length=0) plt.show() 
like image 84
Julien Spronck Avatar answered Oct 09 '22 13:10

Julien Spronck


Thanks for your answers @julien-spronck and @cmidi.
As a note, I had to use both methods to make it work:

import numpy as np import matplotlib.pyplot as plt  fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(11, 3))  data = np.random.random((4, 4))  ax1.imshow(data) ax1.set(title='Bad', ylabel='$A_y$') # plt.setp(ax1.get_xticklabels(), visible=False) # plt.setp(ax1.get_yticklabels(), visible=False) ax1.tick_params(axis='both', which='both', length=0)  ax2.imshow(data) ax2.set(title='Somewhat OK', ylabel='$B_y$') plt.setp(ax2.get_xticklabels(), visible=False) plt.setp(ax2.get_yticklabels(), visible=False) # ax2.tick_params(axis='both', which='both', length=0)  ax3.imshow(data) ax3.set(title='Nice', ylabel='$C_y$') plt.setp(ax3.get_xticklabels(), visible=False) plt.setp(ax3.get_yticklabels(), visible=False) ax3.tick_params(axis='both', which='both', length=0)  plt.show() 

Outcome of the code with desired labels

like image 40
mab Avatar answered Oct 09 '22 13:10

mab