如何在python中隐藏刻度线标签,但将刻度线保持在适当的位置? [英] How to hide ticks label in python but keep the ticks in place?
问题描述
我想在我创建的绘图上隐藏我的刻度标签,但要保留该刻度本身(轴上的小标记).当我尝试使用在此处中找到的内容时,例如整个刻度线被删除,而不仅仅是标签.那我怎样才能只去除标签呢?
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?
推荐答案
设置刻度标签不是空数组,而是空字符串列表.在下面的示例中,我将xtick标签替换为空字符串,而y轴保持不变.如果需要,您可以在此处选择性地保留某些标签.
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()
此代码改编自上一个答案.
这篇关于如何在python中隐藏刻度线标签,但将刻度线保持在适当的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!