How to hide ticks label in python but keep the ticks in place?

独自空忆成欢 提交于 2020-06-24 10:56:52

问题


I want to hide my ticks label on a plot I created, but keep this tick itself (the little marks on the axis). When I try to use what I've found here, for example, the entire tick is removed, and not just the labels. How can I remove only the labels then?


回答1:


Set the tick labels not to be an empty array, but to be a list of empty strings. In the example below, I've replaced the xtick labels with empty strings, leaving the y axis untouched. From here you can selectively keep certain labels if you wished.

import pylab as plt

fig, ax = plt.subplots()
ax.plot([1,2,3],[4,5,6])

labels = [item.get_text() for item in ax.get_xticklabels()]

empty_string_labels = ['']*len(labels)
ax.set_xticklabels(empty_string_labels)

plt.show()

enter image description here

This code is adapted from a previous answer.




回答2:


Here is a slightly simpler answer, using ax.tick_params

import matplotlib.pylab as plt

fig, ax = plt.subplots()
plt.plot([1,2,3],[4,5,6])

ax.tick_params(labelbottom=False)    

plt.show()



回答3:


I didn't find divenex's answer to work for me. This answer creates ticks without triggering any automatic labeling, hence no requirement to use "labelbottom= False":

if the bottom and left ticks already exist:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
plt.plot([1,2,3],[4,5,6])

ax.tick_params(right= True,top= True)

if no ticks already exist:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
plt.plot([1,2,3],[4,5,6])

ax.tick_params(right= True,top= True,left= True, bottom= True)


来源:https://stackoverflow.com/questions/20936658/how-to-hide-ticks-label-in-python-but-keep-the-ticks-in-place

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!